Gathering detailed insights and metrics for @conform-to/validitystate
Gathering detailed insights and metrics for @conform-to/validitystate
Gathering detailed insights and metrics for @conform-to/validitystate
Gathering detailed insights and metrics for @conform-to/validitystate
A type-safe form validation library utilizing web fundamentals to progressively enhance HTML Forms with full support for server frameworks like Remix and Next.js.
npm install @conform-to/validitystate
Typescript
Module System
Node Version
NPM Version
74.5
Supply Chain
99
Quality
75.5
Maintenance
100
Vulnerability
100
License
TypeScript (96.73%)
JavaScript (2.79%)
CSS (0.46%)
Shell (0.01%)
Total Downloads
3,997
Last Day
3
Last Week
29
Last Month
113
Last Year
2,746
2,066 Stars
568 Commits
107 Forks
7 Watching
11 Branches
58 Contributors
Latest Version
0.2.0
Package Id
@conform-to/validitystate@0.2.0
Unpacked Size
52.14 kB
Size
8.31 kB
File Count
8
NPM Version
8.19.2
Node Version
16.18.1
Publised On
09 May 2023
Cumulative downloads
Total Downloads
Last day
200%
3
Compared to previous day
Last week
45%
29
Compared to previous week
Last month
-26.1%
113
Compared to previous month
Last year
119.5%
2,746
Compared to previous year
No dependencies detected.
The current version is not compatible with conform react adapter.
Conform helpers for server validation based on the validation attributes.
A function to parse FormData or URLSearchParams on the server based on the constraints and an optional error formatter.
1import { type FormConstraints, type FormatErrorArgs, parse } from '@conform-to/validitystate'; 2 3const constraints = { 4 email: { type: 'email', required: true }, 5 password: { type: 'password', required: true }, 6 remember: { type: 'checkbox' }, 7} satisify FormConstraints; 8 9function formatError({ input }: FormatErrorArgs) { 10 switch (input.name) { 11 case 'email': { 12 if (input.validity.valueMissing) { 13 return 'Email is required'; 14 } else if (input.validity.typeMismatch) { 15 return 'Email is invalid'; 16 } 17 break; 18 } 19 case 'password': { 20 if (input.validity.valueMissing) { 21 return 'Password is required'; 22 } 23 break; 24 } 25 } 26 27 return ''; 28} 29 30const submission = parse(formData, { 31 constraints, 32 formatError, 33}); 34 35// The error will be a dictinioary mapping input name to the corresponding errors 36// e.g. { email: 'Email is required', password: 'Password is required' } 37console.log(submission.error); 38 39if (!submission.error) { 40 // If no error, the parsed data will be available with the inferred type. 41 // e.g. { email: string; password: string; remember: boolean; } 42 console.log(submission.value); 43}
The error formatter can also return multiple error.
1function formatError({ input }: FormatErrorArgs) { 2 const error = []; 3 4 switch (input.name) { 5 case 'email': { 6 if (input.validity.valueMissing) { 7 error.push('Email is required'); 8 } 9 if (input.validity.typeMismatch) { 10 error.push('Email is invalid'); 11 } 12 break; 13 } 14 case 'password': { 15 if (input.validity.valueMissing) { 16 error.push('Password is required'); 17 } 18 if (input.validity.tooShort) { 19 error.push('Passowrd is too short'); 20 } 21 break; 22 } 23 } 24 25 return error; 26}
If no error formatter is provided, check the defaultFormatError helpers for the default behavior.
A helper to customize client validation by reusing the constraints and error formatter. Error will be set on the form control element using the setCustomValidity
method. It should be called before reporting new error (i.e. triggering form.reportValidity()
).
1import { validate } from '@conform-to/validitystate'; 2 3function Example() { 4 return ( 5 <form 6 onSubmit={(event) => { 7 const form = event.currentTarget; 8 9 // validate before reporting new error 10 validate(form, { 11 constraints, 12 formatError, 13 }); 14 15 if (!form.reportValidity()) { 16 event.preventDefault(); 17 } 18 }} 19 noValidate 20 > 21 {/* ... */} 22 </form> 23 ); 24}
This is the default error formatter used by parse to represent error by all failed validation attributes. For example:
1{ "email": ["required", "type"], "password": ["required"] }
This helper is useful if you want to customize the error based on the default error formatter.
1import { type FormConstraints, type FormatErrorArgs, defaultFormatError } from '@conform-to/validitystate'; 2 3const constraints = { 4 email: { type: 'email', required: true }, 5 password: { type: 'password', required: true }, 6 confirmPassowrd: { type: 'password', required: true }, 7} satisify FormConstraints; 8 9function formatError({ input }: FormatErrorArgs<typeof constraints>) { 10 const error = defaultFormatError({ input }); 11 12 if (input.name === 'confirmPassword' && error.length === 0 && value.password !== value.confirmPassword) { 13 error.push('notmatch'); 14 } 15 16 return error; 17} 18 19const submission = parse(formData, { 20 constraints, 21 formatError, 22});
It gets the actual error messages stored on the validationMessage
property. This is needed if the custom error formatter returns multiple error.
1import { getError } from '@conform-to/validitystate'; 2 3function Example() { 4 const [error, setError] = useState({}); 5 6 return ( 7 <form 8 onInvalid={(event) => { 9 const input = event.target as HTMLInputElement; 10 11 setError((prev) => ({ 12 ...prev, 13 [input.name]: getError(input.validationMessage), 14 })); 15 16 event.preventDefault(); 17 }} 18 > 19 {/* ... */} 20 </form> 21 ); 22}
month
andweek
input type are not implemented due to limited browser support
Support | type | required | minLength | maxLength | pattern | min | max | step | multiple |
---|---|---|---|---|---|---|---|---|---|
text | 🗸 | 🗸 | 🗸 | 🗸 | |||||
🗸 | 🗸 | 🗸 | 🗸 | 🗸 | |||||
password | 🗸 | 🗸 | 🗸 | 🗸 | |||||
url | 🗸 | 🗸 | 🗸 | 🗸 | 🗸 | ||||
tel | 🗸 | 🗸 | 🗸 | 🗸 | |||||
search | 🗸 | 🗸 | 🗸 | 🗸 | |||||
datetime-local | 🗸 | 🗸 | 🗸 | 🗸 | |||||
date | 🗸 | 🗸 | 🗸 | 🗸 | |||||
time | 🗸 | 🗸 | 🗸 | 🗸 | |||||
select | 🗸 | 🗸 | |||||||
textarea | 🗸 | 🗸 | 🗸 | ||||||
radio | 🗸 | ||||||||
color | 🗸 | ||||||||
checkbox | 🗸 | ||||||||
number | 🗸 | 🗸 | 🗸 | 🗸 | |||||
range | 🗸 | 🗸 | 🗸 | 🗸 | |||||
file | 🗸 | 🗸 |
No vulnerabilities found.
No security vulnerabilities found.