0.1.0 • Published 4 years ago

fasst-validation v0.1.0

Weekly downloads
-
License
ISC
Repository
github
Last release
4 years ago

fasst-validation

Npm library to configure and validate objects.

How to use

The library expect an object generated from a json or yaml schema:

firstname:
  - type: minLength
    args: [3]
  - type: maxLength
    args: [5]

lastname:
  - type: minLength
    args: [2]
  - type: maxLength
    args: [5]

email:
  - type: email

each key must containe one or more object with the following properties:

  • type: the validator type
  • args: an (optionnal) array of arguments to feed the validation function. Eg, the minimum length of a minLength validator

After being loaded, the schema can be given to buildValidators, wich return an object with the name of the field for the key and an array of validators.

The validators can be reduced to a single function by calling reduceArrayOfValidators

import validatorsSchema from './validators.yml';
const validators = reduceArrayOfValidators(buildValidators(validatorsSchema));

The resulting validators can then be fed to a FaastForm

return <FasstForm
  values={values}
  setValues={setValues}
  framework={'antd'}
  schema={form}
  observers={{
    firstname: {
      onChange: (v) => console.log('firstname', v.target.value)
    },
    email: {
      onChange: (v) => console.log('email', v.target.value)
    },
    confirm: {
      onClick: onValidate
    }
  }}
  validators={validators}
/>;

Available validators

minLength

Test that a value is at least n characters long. Accept a unique argument to represent the minLength

maxLength

Test that a value is at most n characters long. Accept a unique argument to represent the maxLength

email

Test that a value is an email

phone

Test that a value is a french phone number

siret

Test that a value is a valid siret (based on a luhn check)

Testing

You can run the test with npm test