0.0.51 • Published 5 months ago

@oats-ts/validators v0.0.51

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months 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

5 months ago

0.0.50

5 months ago

0.0.49

1 year ago

0.0.44

1 year ago

0.0.45

1 year ago

0.0.46

1 year ago

0.0.47

1 year ago

0.0.48

1 year ago

0.0.40

2 years ago

0.0.41

2 years ago

0.0.42

2 years ago

0.0.43

2 years ago

0.0.37

2 years ago

0.0.38

2 years ago

0.0.39

2 years ago

0.0.36

2 years ago

0.0.34

2 years ago

0.0.35

2 years ago

0.0.25

2 years ago

0.0.30

2 years ago

0.0.31

2 years ago

0.0.32

2 years ago

0.0.33

2 years ago

0.0.26

2 years ago

0.0.27

2 years ago

0.0.28

2 years ago

0.0.29

2 years ago

0.0.20

2 years ago

0.0.21

2 years ago

0.0.22

2 years ago

0.0.23

2 years ago

0.0.24

2 years ago

0.0.18

2 years ago

0.0.19

2 years ago

0.0.15

2 years ago

0.0.16

2 years ago

0.0.17

2 years ago

0.0.10

2 years ago

0.0.11

2 years ago

0.0.12

2 years ago

0.0.13

2 years ago

0.0.14

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

3 years ago

0.0.3

3 years ago

0.0.4

3 years ago

0.0.2

3 years ago

0.0.0

3 years ago