npm.io
0.0.51 • Published 2 years ago

@oats-ts/validators

Licence
MIT
Version
0.0.51
Deps
0
Size
62 kB
Vulns
0
Weekly
0
Stars
22

@oats-ts/validators

Standalone lightweight runtime validator utility functions used extensively in oats libraries.

  • Created by function composition.
  • Validators NEVER throw, if they do, please report it.
  • Validators returns a list of Issues that can be formatted, and presented to the user.

And an example validator:

import { object, shape, string, number, array, items, boolean, union, lazy } from '@oats-ts/validators'

const personValidator = configure(
  object(
    shape({
      name: string(),
      email: optional(string()),
      occupation: union({
        programmer: literal('programmer'),
        musician: literal('musician'),
        other: literal('other'),
      }),
      married: boolean(),
      friends: array(items(lazy(() => personValidator))),
      address: object(
        shape({
          country: string(),
          zip: number(),
          city: string(),
          street: string(),
        }),
      ),
    }),
  ),
)

When validating simply call this function. It will return a list of Issues, reporting everything that's wrong with the input:

const validPerson = {
  name: 'Test',
  email: 'test',
  occupation: 'other',
  married: false,
  friends: [],
  address: {
    country: 'Test',
    zip: 1243,
    city: 'Test',
    street: 'Test',
  },
}

const invalidPerson = {
  name: false,
  email: 1,
  fr_iends: [{ hi: true }],
}

// Returns an empty array
expect(personValidator(validPerson).length).toBe(0)
// Returns an array with all the issues
expect(personValidator(invalidPerson).length).toBeGreaterThan(0)