-
-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(valibot): Add valibot resolver (#602)
* Add valibot resolver * fix docs * fix node 13 export * remove random lock file
- Loading branch information
1 parent
1a64cd1
commit 2185330
Showing
11 changed files
with
390 additions
and
14 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,18 @@ | ||
{ | ||
"name": "@hookform/resolvers/valibot", | ||
"amdName": "hookformResolversValibot", | ||
"version": "1.0.0", | ||
"private": true, | ||
"description": "React Hook Form validation resolver: valibot", | ||
"main": "dist/valibot.js", | ||
"module": "dist/valibot.module.js", | ||
"umd:main": "dist/valibot.umd.js", | ||
"source": "src/index.ts", | ||
"types": "dist/index.d.ts", | ||
"license": "MIT", | ||
"peerDependencies": { | ||
"react-hook-form": "^7.0.0", | ||
"@hookform/resolvers": "^2.0.0", | ||
"valibot": ">=0.8" | ||
} | ||
} |
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,88 @@ | ||
import { Field, InternalFieldName } from 'react-hook-form'; | ||
import { | ||
object, | ||
string, | ||
minLength, | ||
maxLength, | ||
regex, | ||
number, | ||
minValue, | ||
maxValue, | ||
email, | ||
array, | ||
boolean, | ||
required, | ||
} from 'valibot'; | ||
|
||
export const schema = required( | ||
object({ | ||
username: string([minLength(2), maxLength(30), regex(/^\w+$/)]), | ||
password: string('New Password is required', [ | ||
regex(new RegExp('.*[A-Z].*'), 'One uppercase character'), | ||
regex(new RegExp('.*[a-z].*'), 'One lowercase character'), | ||
regex(new RegExp('.*\\d.*'), 'One number'), | ||
regex( | ||
new RegExp('.*[`~<>?,./!@#$%^&*()\\-_+="\'|{}\\[\\];:\\\\].*'), | ||
'One special character', | ||
), | ||
minLength(8, 'Must be at least 8 characters in length'), | ||
]), | ||
repeatPassword: string('Repeat Password is required'), | ||
accessToken: string('Access token is required'), | ||
birthYear: number('Please enter your birth year', [ | ||
minValue(1900), | ||
maxValue(2013), | ||
]), | ||
email: string([email('Invalid email address')]), | ||
tags: array(string('Tags should be strings')), | ||
enabled: boolean(), | ||
like: required( | ||
object({ | ||
id: number('Like id is required'), | ||
name: string('Like name is required', [minLength(4, 'Too short')]), | ||
}), | ||
), | ||
}), | ||
); | ||
|
||
export const validData = { | ||
username: 'Doe', | ||
password: 'Password123_', | ||
repeatPassword: 'Password123_', | ||
birthYear: 2000, | ||
email: '[email protected]', | ||
tags: ['tag1', 'tag2'], | ||
enabled: true, | ||
accessToken: 'accessToken', | ||
like: { | ||
id: 1, | ||
name: 'name', | ||
}, | ||
}; | ||
|
||
export const invalidData = { | ||
password: '___', | ||
email: '', | ||
birthYear: 'birthYear', | ||
like: { id: 'z' }, | ||
tags: [1, 2, 3], | ||
}; | ||
|
||
export const fields: Record<InternalFieldName, Field['_f']> = { | ||
username: { | ||
ref: { name: 'username' }, | ||
name: 'username', | ||
}, | ||
password: { | ||
ref: { name: 'password' }, | ||
name: 'password', | ||
}, | ||
email: { | ||
ref: { name: 'email' }, | ||
name: 'email', | ||
}, | ||
birthday: { | ||
ref: { name: 'birthday' }, | ||
name: 'birthday', | ||
}, | ||
}; |
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,79 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`valibotResolver > should return a single error from valibotResolver when validation fails 1`] = ` | ||
{ | ||
"errors": { | ||
"accessToken": { | ||
"message": "Invalid type", | ||
"ref": undefined, | ||
"type": "non_optional", | ||
}, | ||
"birthYear": { | ||
"message": "Please enter your birth year", | ||
"ref": undefined, | ||
"type": "number", | ||
}, | ||
"email": { | ||
"message": "Invalid email address", | ||
"ref": { | ||
"name": "email", | ||
}, | ||
"type": "email", | ||
}, | ||
"enabled": { | ||
"message": "Invalid type", | ||
"ref": undefined, | ||
"type": "non_optional", | ||
}, | ||
"like": { | ||
"id": { | ||
"message": "Like id is required", | ||
"ref": undefined, | ||
"type": "number", | ||
}, | ||
"name": { | ||
"message": "Invalid type", | ||
"ref": undefined, | ||
"type": "non_optional", | ||
}, | ||
}, | ||
"password": { | ||
"message": "One uppercase character", | ||
"ref": { | ||
"name": "password", | ||
}, | ||
"type": "regex", | ||
}, | ||
"repeatPassword": { | ||
"message": "Invalid type", | ||
"ref": undefined, | ||
"type": "non_optional", | ||
}, | ||
"tags": [ | ||
{ | ||
"message": "Tags should be strings", | ||
"ref": undefined, | ||
"type": "string", | ||
}, | ||
{ | ||
"message": "Tags should be strings", | ||
"ref": undefined, | ||
"type": "string", | ||
}, | ||
{ | ||
"message": "Tags should be strings", | ||
"ref": undefined, | ||
"type": "string", | ||
}, | ||
], | ||
"username": { | ||
"message": "Invalid type", | ||
"ref": { | ||
"name": "username", | ||
}, | ||
"type": "non_optional", | ||
}, | ||
}, | ||
"values": {}, | ||
} | ||
`; |
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,24 @@ | ||
/* eslint-disable no-console, @typescript-eslint/ban-ts-comment */ | ||
import { valibotResolver } from '..'; | ||
import { schema, validData, fields, invalidData } from './__fixtures__/data'; | ||
|
||
const shouldUseNativeValidation = false; | ||
describe('valibotResolver', () => { | ||
it('should return values from valibotResolver when validation pass', async () => { | ||
const result = await valibotResolver(schema)(validData, undefined, { | ||
fields, | ||
shouldUseNativeValidation, | ||
}); | ||
|
||
expect(result).toEqual({ errors: {}, values: validData }); | ||
}); | ||
|
||
it('should return a single error from valibotResolver when validation fails', async () => { | ||
const result = await valibotResolver(schema)(invalidData, undefined, { | ||
fields, | ||
shouldUseNativeValidation, | ||
}); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './valibot'; | ||
export * from './types'; |
Oops, something went wrong.