1.3.1 • Published 7 years ago

isomorphic-validator v1.3.1

Weekly downloads
3
License
MIT
Repository
github
Last release
7 years ago

Isomorphic Validator

Build Status Coverage Status npm version

:rocket: Share your form validator between client and server

1. Define your validation schema

import { ValueSchemaMapping } from 'isomorphic-validator';

type MyObject = {
  username: string,
  email: string,
  age: number
}

const validationSchema: ValueSchemaMapping<keyof MyObject> = {
  username: {
    required: {},
    hasLength: {
      min: 8,
      max: 30
    }
  },
  email: {
    isEmail: {},
  },
  age: {
    isNumber: {},
    inRange: {
      min: 0,
      max: 150
    }
  }
}

2. Validate

import { createValueValidator, PartialValueValidatorConfig } from 'isomorphic-validator';

const myObject: MyObject = {
  username: 'user',
  email: 'invalidEmail',
  age: -10
}

// Create config or just leave empty to use defaults
const config: PartialValueValidatorConfig = {
  errorMapping: {
    required: {
      notDefined: 'This field is required'
    },
    hasLength: {
      shorter: ({params: {min}}) => `The minimal length is ${min}`,
      longer: ({params: {max}}) => `The maximum length is ${max}`
    },
    isEmail: {
      notValid: 'The email is not valid'
    },
    isNumber: {
      notValid: 'This field has to be a number'
    },
    inRange: {
      underMin: ({params: {min}, target: {name}}) => `The ${name} has to be at least ${min}.`,
      overMax: ({params: {max}, target: {name}}) => `The ${name} has to be smaller than ${max}.`
    }
  }
}

const valueValidator = createValueValidator(config)(schema);
valueValidator(myObject)
  .then((errors) => {
    /*
    errors equals:
    {
      username: {
        hasLength: 'The minimal length is 8',
      },
      email: {
        isEmail: 'The email is not valid',
      },
      age: {
        inRange: 'The age has to be at least 0'
      }
    }
     */
  })
;

3. Share

As the schema is serializable you can easily share it between the client and the server. Isomorphic makes no restrictions which technology you use for that.

API

It is recommended to use this library with typescript. But the usage with javascript is also possible.

You can find all possible validations in validators.ts. The structure of the definitions is the following:

PropertyDescription
inputTypeThe expected input type for this validator
paramsThe parameter this validator needs to accept
casesThe possible result cases (Important for the error messages)
1.3.1

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.8

7 years ago

1.1.7

7 years ago

1.1.6

7 years ago

1.1.5

7 years ago

1.1.4

7 years ago

1.1.3

7 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.0

7 years ago