Skip to content

Latest commit

 

History

History
165 lines (119 loc) · 5.99 KB

_usevalidation_usevalidation_.md

File metadata and controls

165 lines (119 loc) · 5.99 KB

react-uniformed"useValidation"

Module: "useValidation"

Index

Functions

Functions

useValidation

useValidation(validator: SingleValidatorFieldValue›): UseValidatorHookFieldValue

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 SingleValidatorFieldValue A validation map or a validation function.

Returns: UseValidatorHookFieldValue

returns an UseValidatorHook or UseValidatorHookPartial object.

useValidation<T>(validator: T): UseValidatorHookPartialFieldValue, 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: UseValidatorHookPartialFieldValue, T›

returns an UseValidatorHook or UseValidatorHookPartial object.

useValidation<T>(validator: T | SingleValidatorFieldValue›): UseValidatorHookPartialFieldValue, T› | UseValidatorHookFieldValue

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 | SingleValidatorFieldValue

Returns: UseValidatorHookPartialFieldValue, T› | UseValidatorHookFieldValue

returns an UseValidatorHook or UseValidatorHookPartial object.