/yncc

🦌 Ync is a validator and parser inspired by lizod, which in turn was inspired by zod.

Primary LanguageTypeScriptMIT LicenseMIT

Yncc

Yncc is a validator and parser inspired by lizod, which in turn was inspired by zod.

Features

  • API similar to zod, but with a different concept.
  • Tree-shakable
  • CommonJS and ES module support
  • All typescript implementations

Installation

npm install -S yncc

Why is it named Yncc?

Yncc is a library that takes one step back from Zod.

Y ← Z
n ← o
c ← d
c ???

Basic usage

Its writing style is similar to zod, and it has been particularly influenced by lizod.

import { $object, $string, $number, Infer } from "yncc";

// Create a schema
const schema = $object({
  name: $string,
  age: $number,
});

// parse a value
schema.parse({ name: "John", age: 42 });
// => βœ… { success: true, value: { name: 'John', age: 42 } }

schema.parse({ name: "John" });
// => β›” { success: false, errors: [{ path ['age'], kind: 'required' }] }

// type from schema
type Person = Infer<typeof schema>;

Specify it along with the options.

import { $object, $string, $number } from "yncc";

// Create a schema
const schema = $object({
  name: $string({ nullable: true }),
  age: $number({ default: 0, min: 0, max: 100 }),
});

schema.parse({ name: null, age: 42 });
// => βœ… { success: true, value: {name: null, age: 42} }

schema.parse({ name: null });
// => βœ… { success: true, value: {name: null, age: 0} }

schema.parse({ age: 101 });
// => β›” {
//  success: false,
//  errors: [{
//    path: ['name'],
//    kind: 'required',
//  } , {
//    path ['age'],
//    kind: 'optional_validation_failure',
//    option: 'max',
//    optionValue: 100
//  }]}

Yncc is a parser

import { $object, $string, $number } from "yncc";

// Create a schema
const schema = $object({
  name: $string,
  age: $number,
});

// parse a value
schema.parse({ name: 1024, age: "0x20" });
// => βœ… {success: true, value: { name: '1024', age: 32 } }

It has a significantly different concept compared to zod and lizod.
Yncc is a library designed with a focus on parsing. It interprets data as numbers if it can be recognized as number and interprets it as string if it can be recognized as string.

Parsing rules

$number

  • If the value is a string, it will attempt to parse it as a number.
    • βœ… "32" => 32
    • βœ… "0x20" => 32
    • βœ… "0o40" => 32
    • βœ… "0b100000" => 32
    • βœ… "32.0" => 32
    • β›” "Hello world"
    • β›” "32px"
  • Other types will trigger an error.
    • β›” true / false

$boolean

  • If the value is a string, it will attempt to parse it as a boolean.
    • βœ… "true" => true
    • βœ… "TRUE" => true
    • βœ… "false" => false
    • βœ… "FALSE" => false
    • β›” "on"
    • β›” "off"
  • Other types will trigger an error.
    • β›” 0
    • β›” 1

$string

  • If the value is a number, it will attempt to parse it as a string.
    • βœ… 32 => "32"
    • βœ… 32.0 => "32"
    • βœ… 0x20 => "32"
    • βœ… 0o40 => "32"
    • βœ… 0b100000 => "32"
  • If the value is a boolean, it will attempt to parse it as a string.
    • βœ… true => "true"
    • βœ… false => "false"
  • Other types will trigger an error.
    • β›” { a: 1 }
    • β›” [0, 1, 2]

$date

  • If the value is a number, it will attempt to parse it as a string.
    ⚠Warning: Date conversions based on numbers are subject to timezone effects, so please be aware!
    • βœ… 20200101 => "20200101" => Date(2020, 0, 1)
    • βœ… 2020 => "2020" => Date(2020, 0, 1)
    • β›” 101 => "101" => malformed value

All schemas

  • $number
  • $string
  • $boolean
  • $object
  • $array
  • $literal
  • $date
  • $optional
  • $union
  • $intersection

Standard provided validators

  • emailValidator