0.6.9 • Published 9 months ago

validest v0.6.9

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

Typescript validation starter kit

See validest documentation Validation schema for typescript.

Quick start

Installation :

yarn add @validest/core validest

Code :

import { array, object, validate } from '@validest/core';
import { number, oneOf, string } from 'validest';

// Unknown params
const params = {
  name: 'joe',
  profile: {
    name: { firstname: 'john', lastname: 'doe' },
    role: 'ADMIN',
    age: '25',
  },
};

const { name, profile } = validate(
  params,
  object({
    name: string(),
    profile: object({
      name: object({ firstname: string(), lastname: string() }),
      role: string(),
      age: number(),
    }),
  })

  profile.age = 25
);

The returned value of validate function will be typed.

Type schema

Here is the core typed schemas. Each schema is un function wich return a object with data or error.

string

Value will be string with length > 0

const name = validate('hello', string());

oneOf

Parameter takes array of string in parameter ans value will be typed with element of this array.

const role = validate('ADMIN', oneOf(['ADMIN', 'MANAGER'] as Role[]));

number

Value will be a number.

const height = validate('150.5', number());
height = 150.5;

int

Value will be an integer (number type).

const age = validate('25', int());
age = 25;

maybe

Parameter takes another schema to defined the type. Value will be type of the nested schema or undefined.

const age = validate(undefined, maybe(int()));
age = undefined;