-
-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1a64cd1
commit 5b16dd4
Showing
10 changed files
with
430 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
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" | ||
} | ||
} |
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,80 @@ | ||
import { Field, InternalFieldName } from 'react-hook-form'; | ||
import { | ||
object, | ||
string, | ||
minLength, | ||
maxLength, | ||
regex, | ||
number, | ||
minValue, | ||
maxValue, | ||
email, | ||
array, | ||
boolean, | ||
} from 'valibot'; | ||
|
||
export const schema = 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(), | ||
accessToken: string(), | ||
birthYear: number([minValue(1900), maxValue(2013)]), | ||
email: string([email()]), | ||
tags: array(string()), | ||
enabled: boolean(), | ||
like: object({ | ||
id: number(), | ||
name: string([minLength(4)]), | ||
}), | ||
}); | ||
|
||
export const validData = { | ||
username: 'Doe', | ||
password: 'Password123_', | ||
repeatPassword: 'Password123_', | ||
birthYear: 2000, | ||
email: 'john@doe.com', | ||
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": "string", | ||
}, | ||
"birthYear": { | ||
"message": "Invalid type", | ||
"ref": undefined, | ||
"type": "number", | ||
}, | ||
"email": { | ||
"message": "Invalid email", | ||
"ref": { | ||
"name": "email", | ||
}, | ||
"type": "email", | ||
}, | ||
"enabled": { | ||
"message": "Invalid type", | ||
"ref": undefined, | ||
"type": "boolean", | ||
}, | ||
"like": { | ||
"id": { | ||
"message": "Invalid type", | ||
"ref": undefined, | ||
"type": "number", | ||
}, | ||
"name": { | ||
"message": "Invalid type", | ||
"ref": undefined, | ||
"type": "string", | ||
}, | ||
}, | ||
"password": { | ||
"message": "One uppercase character", | ||
"ref": { | ||
"name": "password", | ||
}, | ||
"type": "regex", | ||
}, | ||
"repeatPassword": { | ||
"message": "Invalid type", | ||
"ref": undefined, | ||
"type": "string", | ||
}, | ||
"tags": [ | ||
{ | ||
"message": "Invalid type", | ||
"ref": undefined, | ||
"type": "string", | ||
}, | ||
{ | ||
"message": "Invalid type", | ||
"ref": undefined, | ||
"type": "string", | ||
}, | ||
{ | ||
"message": "Invalid type", | ||
"ref": undefined, | ||
"type": "string", | ||
}, | ||
], | ||
"username": { | ||
"message": "Invalid type", | ||
"ref": { | ||
"name": "username", | ||
}, | ||
"type": "string", | ||
}, | ||
}, | ||
"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.