-
Notifications
You must be signed in to change notification settings - Fork 0
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
c0cb99f
commit 66e03a5
Showing
7 changed files
with
210 additions
and
125 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,8 +56,8 @@ const errors = validateField('Ammar', 'firstName', firstNameRules); | |
#### Methods | ||
| Function | Description | Parameters | Returns | | ||
|-----------------|-------------------------------------------------------|-------------------------------------------------------------------|--------------------| | ||
| `validateField` | Validates a single form field against specified rules.| `value`: The string value of the field.<br>`name`: Name of the field.<br>`rulesObject`: Object containing validation rules. | Array of `FieldError` objects, each containing the `name` of the field and the error `message`. | | ||
| `validateForm` | Validates an entire form. | `values`: A key-value pair object of field names and values.<br>`rulesObject`: A key-value object which maps field names to their rules . | Array of `FieldError` objects for the entire form. | | ||
| `validateField` | Validates a single form field against specified rules.| `value`: The string value of the field.<br>`name`: Name of the field.<br>`Rules`: Object containing validation rules. | Array of `FieldError` objects, each containing the `name` of the field and the error `message`. | | ||
| `validateForm` | Validates an entire form. | `values`: A key-value pair object of field names and values.<br>`Rules`: A key-value object which maps field names to their rules . | Array of `FieldError` objects for the entire form. | | ||
|
||
#### Validation Rules | ||
|
||
|
@@ -78,23 +78,23 @@ const errors = validateField('Ammar', 'firstName', firstNameRules); | |
```Typescript | ||
import { validateField } from 'onsubmit'; | ||
|
||
const rulesObject = { | ||
const Rules = { | ||
minLength: { value: 3, message: 'Minimum length is 3' }, | ||
maxLength: { value: 10, message: 'Maximum length is 10' }, | ||
pattern: { value: /^[a-z]+$/, message: 'Only lowercase letters allowed' }, | ||
custom: { value: (value) => value !== 'example', message: 'Value cannot be "example"' }, | ||
required: { value: true, message: 'Field is required' }, | ||
}; | ||
|
||
const errors = validateField('exampleValue', 'fieldName', rulesObject); | ||
const errors = validateField('exampleValue', 'fieldName', Rules); | ||
``` | ||
|
||
#### Validate an entire form | ||
|
||
```Typescript | ||
import { validateForm } from 'onsubmit'; | ||
|
||
const rulesObject = { | ||
const Rules = { | ||
minLength: { value: 3, message: 'Minimum length is 3' }, | ||
maxLength: { value: 10, message: 'Maximum length is 10' }, | ||
pattern: { value: /^[a-z]+$/, message: 'Only lowercase letters allowed' }, | ||
|
@@ -108,7 +108,7 @@ const values = { | |
email: '[email protected]', | ||
}; | ||
|
||
const errors = validateForm(values, rulesObject); | ||
const errors = validateForm(values, Rules); | ||
``` | ||
|
||
#### Validate a form with a custom rule | ||
|
@@ -117,7 +117,7 @@ const errors = validateForm(values, rulesObject); | |
|
||
import { validateForm } from 'onsubmit'; | ||
|
||
const rulesObject = { | ||
const Rules = { | ||
custom: { value: (value) => value !== 'example', message: 'Value cannot be "example"' }, | ||
}; | ||
|
||
|
@@ -127,14 +127,14 @@ const values = { | |
fieldName3: 'exampleValue3', | ||
}; | ||
|
||
const errors = validateForm(values, rulesObject); | ||
const errors = validateForm(values, Rules); | ||
``` | ||
|
||
## FAQ | ||
|
||
### Which rule object has precedence? | ||
|
||
The `required` rule has the highest precedence. The remaining rules are evaluated in the order they are specified in the `rulesObject`. | ||
The `required` rule has the highest precedence. The remaining rules are evaluated in the order they are specified in the `Rules`. | ||
|
||
|
||
|
||
|
@@ -150,7 +150,7 @@ export interface Rule { | |
message: string; | ||
} | ||
|
||
export interface RulesObject { | ||
export interface Rules { | ||
required?: Rule; | ||
minLength?: Rule; | ||
maxLength?: Rule; | ||
|
@@ -167,12 +167,12 @@ export interface CustomFunction { | |
(value: string): boolean; | ||
} | ||
|
||
export interface FormDataObject { | ||
export interface KeyValuePair { | ||
[key: string]: string | FormDataEntryValue; | ||
} | ||
|
||
export interface NameRuleMap { | ||
[key: string]: RulesObject; | ||
[key: string]: Rules; | ||
}; | ||
|
||
export type ConfigMap = { | ||
|
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
Oops, something went wrong.