react-uniformed › "useValidation"
▸ useValidation(validator
: SingleValidator‹FieldValue›): UseValidatorHook‹FieldValue›
A hook for performing validation.
See useErrors.
example
// validate using validation maps
const { validateByName, errors } = useValidation({
name: (value) => (value ? '' : 'name is required!'),
email: (value) => (value ? '' : 'email is required!'),
});
// "email is required!"
await validateByName('email', '');
// {email: "email is required!"}
console.log(errors);
// validate with one validation function
const { errors, validate } = useValidation((values) => {
const errors = { name: '', email: '' };
if (!values.name) {
errors.name = 'name is required!';
}
if (!values.email) {
errors.email = 'email is required!';
}
return errors;
});
// {name: "", email: "email is required!"}
await validate({ name: 'John' });
// {name: "", email: "email is required!"}
console.log(errors);
Parameters:
Name | Type | Description |
---|---|---|
validator |
SingleValidator‹FieldValue› | A validation map or a validation function. |
Returns: UseValidatorHook‹FieldValue›
returns an UseValidatorHook or UseValidatorHookPartial object.
▸ useValidation<T>(validator
: T): UseValidatorHookPartial‹FieldValue, T›
A hook for performing validation.
See useErrors.
example
// validate using validation maps
const { validateByName, errors } = useValidation({
name: (value) => (value ? '' : 'name is required!'),
email: (value) => (value ? '' : 'email is required!'),
});
// "email is required!"
await validateByName('email', '');
// {email: "email is required!"}
console.log(errors);
// validate with one validation function
const { errors, validate } = useValidation((values) => {
const errors = { name: '', email: '' };
if (!values.name) {
errors.name = 'name is required!';
}
if (!values.email) {
errors.email = 'email is required!';
}
return errors;
});
// {name: "", email: "email is required!"}
await validate({ name: 'John' });
// {name: "", email: "email is required!"}
console.log(errors);
Type parameters:
▪ T: Validators
Parameters:
Name | Type |
---|---|
validator |
T |
Returns: UseValidatorHookPartial‹FieldValue, T›
returns an UseValidatorHook or UseValidatorHookPartial object.
▸ useValidation<T>(validator
: T | SingleValidator‹FieldValue›): UseValidatorHookPartial‹FieldValue, T› | UseValidatorHook‹FieldValue›
A hook for performing validation.
See useErrors.
example
// validate using validation maps
const { validateByName, errors } = useValidation({
name: (value) => (value ? '' : 'name is required!'),
email: (value) => (value ? '' : 'email is required!'),
});
// "email is required!"
await validateByName('email', '');
// {email: "email is required!"}
console.log(errors);
// validate with one validation function
const { errors, validate } = useValidation((values) => {
const errors = { name: '', email: '' };
if (!values.name) {
errors.name = 'name is required!';
}
if (!values.email) {
errors.email = 'email is required!';
}
return errors;
});
// {name: "", email: "email is required!"}
await validate({ name: 'John' });
// {name: "", email: "email is required!"}
console.log(errors);
Type parameters:
▪ T: Validators
Parameters:
Name | Type |
---|---|
validator |
T | SingleValidator‹FieldValue› |
Returns: UseValidatorHookPartial‹FieldValue, T› | UseValidatorHook‹FieldValue›
returns an UseValidatorHook or UseValidatorHookPartial object.