composable-validation
TypeScript icon, indicating that this package has built-in type declarations

0.4.0 • Public • Published

composable-validation

Npm CircleCI Greenkeeper badge

A declarative and functional way to do validation

Installation

yarn add composable-validation

Usage

import { required, onlyIf, optional, validate, ValueValidator } from 'composable-validation'
import { includes } from 'lodash'
import { lessThan, minLength } from 'composable-validation/dist/text'

const includes = <T>(requiredValue: T): ValueValidator<Array<T>> => (values) =>
  includes(values, requiredValue) ? [`Must include ${requiredValue}`] : []

type AnimalObject ={
  ant: string,
  bat: number,
  cat: {
    age: number,
    favoriteFoods: Array<string>,
  },
}

// Create your validator by combining validators and combinators
const animalValidator = {
  ant: optional(minLength(6)),
  bat: required(lessThan(10)),
  cat: {
    age: lessThan(6),
    favoriteFoods: includes('fish'),
  }
}

// Validate something

console.log(validate(animalValidator, {
  bat: 24,
  cat: {
    age: 4,
    favoriteFoods: ['cheese'],
  },
}))
// { cat: { age: [ 'Must be at most 6' ] } }


console.log(validate(animalValidator, {
  ant: '12345',
  cat: {
    favoriteFoods: ['fish'],
  },
}))
// { 
//   ant: ['Must be at least 6 in length'],
//   bat: ['Please complete this field'],
//   cat: { 
//     age: ['Please complete this field'],
//     favoriteFoods: ['Must include fish'],
//   }
// }

Readme

Keywords

none

Package Sidebar

Install

npm i composable-validation

Weekly Downloads

6

Version

0.4.0

License

MIT

Unpacked Size

10.1 kB

Total Files

14

Last publish

Collaborators

  • dean177