-
Notifications
You must be signed in to change notification settings - Fork 20
[JSON-Logic] Part 0: Allow consts to support single values only #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
674943b
feat: allow consts to support single values only
brennj e872fdc
chore: custom error message test
brennj ff0d02b
Release 0.4.4-dev.20230829102031
brennj 0317062
Revert "Release 0.4.4-dev.20230829102031"
brennj 143a3d9
chore: feedback from sandrina
brennj 2c2c524
Release 0.4.4-dev.20230829191632
brennj 4bdcf84
Revert "Release 0.4.4-dev.20230829191632"
brennj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,86 @@ | ||
| import { createHeadlessForm } from '../createHeadlessForm'; | ||
|
|
||
| describe('validations: const', () => { | ||
| it('Should work for number', () => { | ||
| const { handleValidation } = createHeadlessForm( | ||
| { | ||
| properties: { | ||
| ten_only: { type: 'number', const: 10 }, | ||
| }, | ||
| }, | ||
| { strictInputType: false } | ||
| ); | ||
| expect(handleValidation({}).formErrors).toEqual(undefined); | ||
| expect(handleValidation({ ten_only: 1 }).formErrors).toEqual({ | ||
| ten_only: 'The only accepted value is 10.', | ||
| }); | ||
| expect(handleValidation({ ten_only: 10 }).formErrors).toBeUndefined(); | ||
| // null is also considered valid until we fix @BUG RMT-518 | ||
| // Expectation: To fail with error "The only accepted value is 10." | ||
| expect(handleValidation({ ten_only: null }).formErrors).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('Should work for text', () => { | ||
| const { handleValidation } = createHeadlessForm( | ||
| { | ||
| properties: { | ||
| hello_only: { type: 'string', const: 'hello' }, | ||
| }, | ||
| }, | ||
| { strictInputType: false } | ||
| ); | ||
| expect(handleValidation({}).formErrors).toEqual(undefined); | ||
| expect(handleValidation({ hello_only: 'what' }).formErrors).toEqual({ | ||
| hello_only: 'The only accepted value is hello.', | ||
| }); | ||
| expect(handleValidation({ hello_only: 'hello' }).formErrors).toEqual(undefined); | ||
| }); | ||
|
|
||
| it('Should work for a conditionally applied const', () => { | ||
| const { handleValidation } = createHeadlessForm( | ||
| { | ||
| properties: { | ||
| answer: { type: 'string', oneOf: [{ const: 'yes' }, { const: 'no' }] }, | ||
| amount: { | ||
| description: 'If you select yes, this needs to be exactly 10.', | ||
| type: 'number', | ||
| }, | ||
| }, | ||
| allOf: [ | ||
| { | ||
| if: { properties: { answer: { const: 'yes' } }, required: ['answer'] }, | ||
| then: { properties: { amount: { const: 10 } }, required: ['amount'] }, | ||
| }, | ||
| ], | ||
| }, | ||
| { strictInputType: false } | ||
| ); | ||
| expect(handleValidation({}).formErrors).toEqual(undefined); | ||
| expect(handleValidation({ answer: 'no' }).formErrors).toEqual(undefined); | ||
| expect(handleValidation({ answer: 'yes' }).formErrors).toEqual({ amount: 'Required field' }); | ||
| expect(handleValidation({ answer: 'yes', amount: 1 }).formErrors).toEqual({ | ||
| amount: 'The only accepted value is 10.', | ||
| }); | ||
| expect(handleValidation({ answer: 'yes', amount: 10 }).formErrors).toEqual(undefined); | ||
| }); | ||
|
|
||
| it('Should show the custom error message', () => { | ||
| const { handleValidation } = createHeadlessForm( | ||
| { | ||
| properties: { | ||
| string: { | ||
| type: 'string', | ||
| const: 'hello', | ||
| 'x-jsf-errorMessage': { const: 'You must say hello!!!' }, | ||
| }, | ||
| }, | ||
| }, | ||
| { strictInputType: false } | ||
| ); | ||
| expect(handleValidation({}).formErrors).toEqual(undefined); | ||
| expect(handleValidation({ string: 'hi' }).formErrors).toEqual({ | ||
| string: 'You must say hello!!!', | ||
| }); | ||
| expect(handleValidation({ string: 'hello' }).formErrors).toEqual(undefined); | ||
| }); | ||
| }); | ||
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.