-
-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Joi sync validation (#116)
* feat: add Joi sync validation * fix(joi): sync error * test: add validateAllCriteria test * test(joi): update test description
- Loading branch information
Showing
4 changed files
with
159 additions
and
5 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 |
---|---|---|
|
@@ -38,8 +38,34 @@ describe('joiResolver', () => { | |
enabled: true, | ||
}; | ||
|
||
const validateAsyncSpy = jest.spyOn(schema, 'validateAsync'); | ||
const validateSpy = jest.spyOn(schema, 'validate'); | ||
|
||
const result = await joiResolver(schema)(data); | ||
|
||
expect(validateSpy).not.toHaveBeenCalled(); | ||
expect(validateAsyncSpy).toHaveBeenCalledTimes(1); | ||
expect(result).toEqual({ errors: {}, values: data }); | ||
}); | ||
|
||
it('should return values from joiResolver with `mode: sync` when validation pass', async () => { | ||
const data: Data = { | ||
username: 'Doe', | ||
password: 'Password123', | ||
repeatPassword: 'Password123', | ||
birthYear: 2000, | ||
email: '[email protected]', | ||
tags: ['tag1', 'tag2'], | ||
enabled: true, | ||
}; | ||
|
||
const validateAsyncSpy = jest.spyOn(schema, 'validateAsync'); | ||
const validateSpy = jest.spyOn(schema, 'validate'); | ||
|
||
const result = await joiResolver(schema, undefined, { mode: 'sync' })(data); | ||
|
||
expect(validateAsyncSpy).not.toHaveBeenCalled(); | ||
expect(validateSpy).toHaveBeenCalledTimes(1); | ||
expect(result).toEqual({ errors: {}, values: data }); | ||
}); | ||
|
||
|
@@ -55,6 +81,23 @@ describe('joiResolver', () => { | |
expect(result).toMatchSnapshot(); | ||
}); | ||
|
||
it('should return a single error from joiResolver with `mode: sync` when validation fails', async () => { | ||
const data = { | ||
password: '___', | ||
email: '', | ||
birthYear: 'birthYear', | ||
}; | ||
|
||
const validateAsyncSpy = jest.spyOn(schema, 'validateAsync'); | ||
const validateSpy = jest.spyOn(schema, 'validate'); | ||
|
||
const result = await joiResolver(schema, undefined, { mode: 'sync' })(data); | ||
|
||
expect(validateAsyncSpy).not.toHaveBeenCalled(); | ||
expect(validateSpy).toHaveBeenCalledTimes(1); | ||
expect(result).toMatchSnapshot(); | ||
}); | ||
|
||
it('should return all the errors from joiResolver when validation fails with `validateAllFieldCriteria` set to true', async () => { | ||
const data = { | ||
password: '___', | ||
|
@@ -66,4 +109,20 @@ describe('joiResolver', () => { | |
|
||
expect(result).toMatchSnapshot(); | ||
}); | ||
|
||
it('should return all the errors from joiResolver when validation fails with `validateAllFieldCriteria` set to true and `mode: sync`', async () => { | ||
const data = { | ||
password: '___', | ||
email: '', | ||
birthYear: 'birthYear', | ||
}; | ||
|
||
const result = await joiResolver(schema, undefined, { mode: 'sync' })( | ||
data, | ||
undefined, | ||
true, | ||
); | ||
|
||
expect(result).toMatchSnapshot(); | ||
}); | ||
}); |
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