-
-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* update test and resolvers * Update README.md Co-authored-by: Joris <reixjoris@gmail.com> * Update src/vest.ts Co-authored-by: Joris <reixjoris@gmail.com> * Update vest.ts * Update README.md Co-authored-by: Joris <reixjoris@gmail.com> * Update vest.ts * fix missing files with vest * update package.json to include vest files * Update src/vest.ts Co-authored-by: Evyatar <code@ealush.com> * fix readme * Update src/vest.ts Co-authored-by: Evyatar <code@ealush.com> * Revert "Update src/vest.ts" This reverts commit 1b2d12f. * include promisfy * exclude throw from promisfy Co-authored-by: Joris <reixjoris@gmail.com> Co-authored-by: Evyatar <code@ealush.com>
- Loading branch information
1 parent
634dab7
commit 5a868d4
Showing
8 changed files
with
243 additions
and
3 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
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,120 @@ | ||
import * as vest from 'vest'; | ||
import { vestResolver } from './vest'; | ||
|
||
const validationSuite = vest.create('form', (data: any = {}) => { | ||
vest.test('username', 'Username is required', () => { | ||
vest.enforce(data.username).isNotEmpty(); | ||
}); | ||
|
||
vest.test('username', 'Must be longer than 3 chars', () => { | ||
vest.enforce(data.username).longerThan(3); | ||
}); | ||
|
||
vest.test('deepObject.data', 'deepObject.data is required', () => { | ||
vest.enforce(data.deepObject.data).isNotEmpty(); | ||
}); | ||
|
||
vest.test('password', 'Password is required', () => { | ||
vest.enforce(data.password).isNotEmpty(); | ||
}); | ||
|
||
vest.test('password', 'Password must be at least 5 chars', () => { | ||
vest.enforce(data.password).longerThanOrEquals(5); | ||
}); | ||
|
||
vest.test('password', 'Password must contain a digit', () => { | ||
vest.enforce(data.password).matches(/[0-9]/); | ||
}); | ||
|
||
vest.test('password', 'Password must contain a symbol', () => { | ||
vest.enforce(data.password).matches(/[^A-Za-z0-9]/); | ||
}); | ||
}); | ||
|
||
describe('vest', () => { | ||
it('should return values from vestResolver when validation pass', async () => { | ||
const data = { | ||
username: 'asdda', | ||
password: 'asddfg123!', | ||
deepObject: { | ||
data: 'test', | ||
}, | ||
}; | ||
expect(await vestResolver(validationSuite)(data, {})).toEqual({ | ||
values: data, | ||
errors: {}, | ||
}); | ||
}); | ||
|
||
it('should return single error message from vestResolver when validation fails and validateAllFieldCriteria set to false', async () => { | ||
const data = { | ||
username: '', | ||
password: 'a', | ||
deepObject: { | ||
data: '', | ||
}, | ||
}; | ||
|
||
expect(await vestResolver(validationSuite)(data, {})).toEqual({ | ||
values: {}, | ||
errors: { | ||
username: { | ||
type: '', | ||
message: 'Username is required', | ||
}, | ||
password: { | ||
type: '', | ||
message: 'Password must be at least 5 chars', | ||
}, | ||
deepObject: { | ||
data: { | ||
type: '', | ||
message: 'deepObject.data is required', | ||
}, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
it('should return all the error messages from vestResolver when validation fails and validateAllFieldCriteria set to true', async () => { | ||
const data = { | ||
username: '', | ||
password: 'a', | ||
deepObject: { | ||
data: '', | ||
}, | ||
}; | ||
|
||
expect(await vestResolver(validationSuite, {}, true)(data, {})).toEqual({ | ||
values: {}, | ||
errors: { | ||
username: { | ||
type: '', | ||
message: 'Username is required', | ||
types: { | ||
0: 'Username is required', | ||
1: 'Must be longer than 3 chars', | ||
}, | ||
}, | ||
password: { | ||
type: '', | ||
message: 'Password must be at least 5 chars', | ||
types: { | ||
0: 'Password must be at least 5 chars', | ||
1: 'Password must contain a digit', | ||
2: 'Password must contain a symbol', | ||
}, | ||
}, | ||
deepObject: { | ||
data: { | ||
type: '', | ||
message: 'deepObject.data is required', | ||
types: { | ||
0: 'deepObject.data is required', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
}); | ||
}); |
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,59 @@ | ||
import { FieldValues, Resolver, transformToNestObject } from 'react-hook-form'; | ||
import * as Vest from 'vest'; | ||
|
||
type VestErrors = Record<string, string[]>; | ||
|
||
type ICreateResult = ReturnType<typeof Vest.create>; | ||
|
||
type Promisify = <T extends ICreateResult, K>( | ||
fn: T, | ||
) => (args: K) => Promise<Vest.IVestResult>; | ||
|
||
const promisify: Promisify = (validatorFn) => (...args) => | ||
new Promise((resolve) => validatorFn(...args).done(resolve as Vest.DoneCB)); | ||
|
||
const parseErrorSchema = ( | ||
vestError: VestErrors, | ||
validateAllFieldCriteria: boolean, | ||
) => { | ||
return Object.entries(vestError).reduce((prev, [key, value]) => { | ||
return { | ||
...prev, | ||
[key]: { | ||
type: '', | ||
message: value[0], | ||
...(validateAllFieldCriteria | ||
? { | ||
types: value.reduce((prev, message, index) => { | ||
return { | ||
...prev, | ||
[index]: message, | ||
}; | ||
}, {}), | ||
} | ||
: {}), | ||
}, | ||
}; | ||
}, {}); | ||
}; | ||
|
||
export const vestResolver = <TFieldValues extends FieldValues>( | ||
schema: ICreateResult, | ||
_: any = {}, | ||
validateAllFieldCriteria = false, | ||
): Resolver<TFieldValues> => async (values) => { | ||
const validateSchema = promisify(schema); | ||
const result = await validateSchema(values); | ||
const errors = result.getErrors(); | ||
|
||
if (!result.hasErrors()) { | ||
return { values: values as any, errors: {} }; | ||
} | ||
|
||
return { | ||
values: {}, | ||
errors: transformToNestObject( | ||
parseErrorSchema(errors, validateAllFieldCriteria), | ||
), | ||
}; | ||
}; |
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 @@ | ||
export * from './dist/vest'; |
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 @@ | ||
module.exports = require('./dist/vest'); |
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