0.0.51 • Published 1 year ago

@oats-ts/validators v0.0.51

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

@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)
0.0.51

1 year ago

0.0.50

2 years ago

0.0.49

3 years ago

0.0.44

3 years ago

0.0.45

3 years ago

0.0.46

3 years ago

0.0.47

3 years ago

0.0.48

3 years ago

0.0.40

3 years ago

0.0.41

3 years ago

0.0.42

3 years ago

0.0.43

3 years ago

0.0.37

3 years ago

0.0.38

3 years ago

0.0.39

3 years ago

0.0.36

3 years ago

0.0.34

3 years ago

0.0.35

3 years ago

0.0.25

3 years ago

0.0.30

3 years ago

0.0.31

3 years ago

0.0.32

3 years ago

0.0.33

3 years ago

0.0.26

3 years ago

0.0.27

3 years ago

0.0.28

3 years ago

0.0.29

3 years ago

0.0.20

3 years ago

0.0.21

3 years ago

0.0.22

3 years ago

0.0.23

3 years ago

0.0.24

3 years ago

0.0.18

3 years ago

0.0.19

3 years ago

0.0.15

3 years ago

0.0.16

3 years ago

0.0.17

3 years ago

0.0.10

3 years ago

0.0.11

3 years ago

0.0.12

3 years ago

0.0.13

3 years ago

0.0.14

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.5

4 years ago

0.0.3

4 years ago

0.0.4

4 years ago

0.0.2

4 years ago

0.0.0

4 years ago