Skip to content

Tiny laravel-like data validator for react.

License

Notifications You must be signed in to change notification settings

Ogimle/use-validatorjs

 
 

Repository files navigation

Ci Status Npm version React version

use-validatorjs

Package includes its TypeScript Definitions

Install

npm install use-validatorjs

Usage

import useValidator from 'use-validatorjs';

// Some i18n function like https://www.i18next.com/overview/api#t
function t(key, params) {
  return key;
}

const myAwesomeValidation (t, val) => {
  if (val !== 'strict-rule') {
    return t('myAwesomeValidation');
  }
}

function App() {
  const data = {
    some: '',
    other: 3,
  };

  const { isErrors, isShowErrors, errors, showErrors, validation } = useValidator(t, data, {
    some: required,
    other: [required, min(2), myAwesomeValidation],
  });

  return (
    <div>
      <div>
        <div>Some: {data.some}</div>
        {errors.some && <div styles={{ color: 'red' }}>{errors.some}</div>}
      </div>
      <div>
        <div>Other: {data.other}</div>
        {errors.other && <div styles={{ color: 'red' }}>{errors.other}</div>}
      </div>
    </div>
  );
}

Development

npm run format # code fomatting
npm run lint # linting
npm run test # testing

Active maintenance with care and ❤️.

Feel free to send a PR.

About

Tiny laravel-like data validator for react.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 100.0%