-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement literal schema and rules
- Loading branch information
1 parent
e7f6a87
commit e0654f2
Showing
4 changed files
with
181 additions
and
13 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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* @vinejs/vine | ||
* | ||
* (c) VineJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import { test } from '@japa/runner' | ||
import vine from '../../../index.js' | ||
|
||
test.group('Literal', () => { | ||
test('fail when value is not same as the expected value', async ({ assert }) => { | ||
const schema = vine.object({ | ||
is_hiring_guide: vine.literal(true), | ||
}) | ||
|
||
const data = { | ||
is_hiring_guide: false, | ||
} | ||
|
||
await assert.validationErrors(vine.validate({ schema, data }), [ | ||
{ | ||
field: 'is_hiring_guide', | ||
message: 'The is_hiring_guide field must be true', | ||
rule: 'literal', | ||
meta: { | ||
expectedValue: true, | ||
}, | ||
}, | ||
]) | ||
}) | ||
|
||
test('pass when value is same as the expected value', async ({ assert }) => { | ||
const schema = vine.object({ | ||
is_hiring_guide: vine.literal(true), | ||
}) | ||
|
||
const data = { | ||
is_hiring_guide: 'true', | ||
} | ||
|
||
await assert.validationOutput(vine.validate({ schema, data }), { | ||
is_hiring_guide: true, | ||
}) | ||
}) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* @vinejs/vine | ||
* | ||
* (c) VineJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import { test } from '@japa/runner' | ||
import { validator } from '../../../factories/main.js' | ||
import { equalsRule } from '../../../src/schema/literal/rules.js' | ||
|
||
test.group('Literal | equals', () => { | ||
test('report when input value is not same as expected value', () => { | ||
const equals = equalsRule({ expectedValue: 1 }) | ||
const validated = validator.execute(equals, 'foo') | ||
|
||
validated.assertError('The dummy field must be 1') | ||
}) | ||
|
||
test('pass when input value is same as expected value', () => { | ||
const equals = equalsRule({ expectedValue: 1 }) | ||
const validated = validator.execute(equals, 1) | ||
|
||
validated.assertSucceeded() | ||
validated.assertOutput(1) | ||
}) | ||
|
||
test('normalize input value when expected value is a number', () => { | ||
const equals = equalsRule({ expectedValue: 1 }) | ||
const validated = validator.execute(equals, '1') | ||
|
||
validated.assertSucceeded() | ||
validated.assertOutput(1) | ||
}) | ||
|
||
test('normalize input value when expected value is a boolean', () => { | ||
const equals = equalsRule({ expectedValue: false }) | ||
const validated = validator.execute(equals, 'false') | ||
validated.assertSucceeded() | ||
validated.assertOutput(false) | ||
|
||
const equals1 = equalsRule({ expectedValue: true }) | ||
const validated1 = validator.execute(equals1, 'true') | ||
validated1.assertSucceeded() | ||
validated1.assertOutput(true) | ||
}) | ||
}) |
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