3.0.0 • Published 3 years ago

mandle v3.0.0

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

Mandle

CircleCI npm codecov

Mandle is a functional validation library built in TypeScript. It brings a simple, declarative api for validating your data.

It's tiny in size and has no dependencies.

Installation

yarn add mandle

Example

import { makeValidator } from 'mandle'
import { required, atLeast18 } from './constraints'

// Make a validator instance
const validatePerson = makeValidator({
  name: [required],
  age: [required, atLeast18],
})

// Validate some data
const result = validatePerson(
  {
    name: 'Foo Fooson',
    age: 16,
  },
)

// {
//   age: "Must be at least 18"
// }

Constraints

To construct a validator you should give it some constraints. A constraint is a function that takes a value and returns either a string (the validation error message) or undefined.

For example, one of the simplest constraints you might want is required:

const required = (val: any) => (val ? undefined : 'Required')

The data being validated is also provided as the 2nd argument for a constraint for situations where your constaint depends on some other value in the data. E.g.

const passwordEquals: Constraint<PasswordFields> = (password, fields) =>
  password !== fields.passwordConfirm ? 'Passwords should match' : undefined
3.0.0

3 years ago

2.3.0

4 years ago

2.2.1

4 years ago

2.2.0

6 years ago

2.1.0

6 years ago

2.0.0

6 years ago

1.2.0

6 years ago

1.1.0

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago