0.4.5 • Published 2 years ago

@ts-v/kit v0.4.5

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Typescript validation starter kit

See ts-v documentation Validation schema for typescript.

Quick start

Installation :

yarn add @ts-v/core @ts-v/kit

Code :

import { array, object, validate } from '@ts-v/core';
import { number, oneOf, string } from '@ts-v/kit';

// 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;
0.4.5

2 years ago

0.4.4

2 years ago

0.4.1

2 years ago

0.4.3

2 years ago

0.4.2

2 years ago

0.4.0

2 years ago

0.3.1

2 years ago

0.3.0

2 years ago

0.2.6

2 years ago

0.2.5

2 years ago

0.2.4

2 years ago

0.2.3

2 years ago

0.2.2

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.10

2 years ago