-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Harminder Virk
authored and
Harminder Virk
committed
Jun 2, 2024
1 parent
39204e4
commit cebb8e0
Showing
2 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,8 @@ import vine, { | |
VineLiteral, | ||
VineBoolean, | ||
} from '../../index.js' | ||
import { Infer } from '../../src/types.js' | ||
import { ValidationError } from '../../src/errors/validation_error.js' | ||
|
||
test.group('Validator | metadata', () => { | ||
test('pass metadata to the validation pipeline', async ({ assert }) => { | ||
|
@@ -314,3 +316,79 @@ test.group('Validator | toJSON', () => { | |
`) | ||
}) | ||
}) | ||
|
||
test.group('Validator | tryValidator', () => { | ||
test('return validation errors without throwing an exception', async ({ | ||
assert, | ||
expectTypeOf, | ||
}) => { | ||
const author = vine.object({ | ||
name: vine.string(), | ||
email: vine.string().email(), | ||
}) | ||
|
||
const validator = vine.compile(author) | ||
const [error, result] = await validator.tryValidate({}) | ||
assert.instanceOf(error, ValidationError) | ||
assert.isNull(result) | ||
|
||
if (error) { | ||
expectTypeOf(result).toMatchTypeOf(null) | ||
expectTypeOf(error).toMatchTypeOf<ValidationError>() | ||
} | ||
if (result) { | ||
expectTypeOf(error).toMatchTypeOf(null) | ||
expectTypeOf(result).toMatchTypeOf<Infer<typeof validator>>() | ||
} | ||
}) | ||
|
||
test('rethrow non ValidationError errors', async () => { | ||
const author = vine.object({ | ||
name: vine.string(), | ||
email: vine.string().email(), | ||
}) | ||
|
||
const validator = vine | ||
.withMetaData<{ choices: string[] }>(() => { | ||
throw new Error('Invalid metadata') | ||
}) | ||
.compile(author) | ||
|
||
await validator.tryValidate( | ||
{}, | ||
{ | ||
meta: { | ||
choices: [], | ||
}, | ||
} | ||
) | ||
}).throws('Invalid metadata') | ||
|
||
test('return validated data', async ({ assert, expectTypeOf }) => { | ||
const author = vine.object({ | ||
name: vine.string(), | ||
email: vine.string().email(), | ||
}) | ||
|
||
const validator = vine.compile(author) | ||
const [error, result] = await validator.tryValidate({ | ||
name: 'virk', | ||
email: '[email protected]', | ||
}) | ||
|
||
assert.isNull(error) | ||
assert.deepEqual(result, { | ||
name: 'virk', | ||
email: '[email protected]', | ||
}) | ||
|
||
if (error) { | ||
expectTypeOf(result).toMatchTypeOf(null) | ||
expectTypeOf(error).toMatchTypeOf<ValidationError>() | ||
} | ||
if (result) { | ||
expectTypeOf(error).toMatchTypeOf(null) | ||
expectTypeOf(result).toMatchTypeOf<Infer<typeof validator>>() | ||
} | ||
}) | ||
}) |