11# Tiny Schema Validator
22
3- small, practical, and type-safe Schema validator .
3+ JSON schema validator with great type inference .
44
55[ ![ GitHub license] ( https://img.shields.io/github/license/5alidz/tiny-schema-validator )] ( https://github.com/5alidz/tiny-schema-validator/blob/master/LICENSE ) ![ Minzipped size] ( https://img.shields.io/bundlephobia/minzip/tiny-schema-validator.svg )
66
7- ## History
8-
9- This started as a side-project for me to learn about advanced TypeScript topics and was never intended to be an npm package,
10- but I liked how it turned up and decided that it might be useful to use in my future projects.
11-
127## Installation
138
149``` sh
15- # npm
1610npm install tiny-schema-validator
17-
18- # yarn
11+ # or
1912yarn add tiny-schema-validator
2013```
2114
@@ -27,13 +20,21 @@ yarn add tiny-schema-validator
2720import { createSchema , _ } from ' tiny-schema-validator' ;
2821
2922export const Person = createSchema ({
30- name: _ .string ({ maxLength: [100 , ' too-long' ], minLength: [2 , ' too-short' ] }),
31- age: _ .number ({ max: [150 , ' too-old' ], min: [13 , ' too-young' ]}),
32- email: _ .string ({ pattern: [/ ^ [^ @] + @[^ @] + \. [^ @] + $ / , ' invalid-email' ]});
23+ name: _ .string ({
24+ maxLength: [100 , ' too-long' ],
25+ minLength: [2 , ' too-short' ],
26+ }),
27+ age: _ .number ({
28+ max: [150 , ' too-old' ],
29+ min: [13 , ' too-young' ],
30+ }),
31+ email: _ .string ({
32+ pattern: [/ ^ [^ @] + @[^ @] + \. [^ @] + $ / , ' invalid-email' ],
33+ });
3334});
3435```
3536
36- and in TypeScript everything is the same, but to get the data type inferred from the schema you can do this:
37+ and in TypeScript, everything is the same, but to get the data type inferred from the schema, you can do this:
3738
3839``` ts
3940// PersonType { name: string; age: number; email: string; }
@@ -87,45 +88,45 @@ validators.string(); // creates a string validator
8788
8889Check out the full validators API below:
8990
90- | validator | signature | props |
91- | :-------- | ------------------------------- | :----------------------------------------------------------- |
92- | string | ` string(options?) ` | options (optional): Object |
93- | | | - ` optional ` boolean defaults to false |
94- | | | - ` maxLength ` [ length: number, error: string] |
95- | | | - ` minLength ` [ length: number, error: string] |
96- | | | - ` pattern ` [ pattern: RegExp, error: string] |
97- | | | |
98- | number | ` number(options?) ` | options(optional): Object |
99- | | | - ` optional ` boolean default to false |
100- | | | - ` min ` [ number, error: string] |
101- | | | - ` max ` [ number, error: string] |
102- | | | - ` is ` [ 'integer' \ | 'float', error: string] default is both |
103- | | | |
104- | boolean | ` boolean(options?) ` | options(optional): Object |
105- | | | - ` optional ` boolean default to false |
106- | | | |
107- | list | ` list(validators[], options?) ` | validators: Array of validators |
108- | | | options(optional): Object |
109- | | | - ` optional ` boolean default to false |
110- | | | |
111- | listof | ` listof(validator, options?) ` | validator: Validator |
112- | | | options(optional): Object |
113- | | | - ` optional ` boolean default to false |
114- | | | |
115- | record | ` record(shape, options?) ` | shape: Object ` { [key: string]: Validator } ` |
116- | | | options(optional): Object |
117- | | | - ` optional ` boolean default to false |
118- | | | |
119- | recordof | ` recordof(validator, options?) ` | validator: Validator |
120- | | | options(optional): Object |
121- | | | - ` optional ` boolean default to false |
91+ | validator | signature | props |
92+ | :-------- | ------------------------------- | :------------------------------------------------------------ |
93+ | string | ` string(options?) ` | options (optional): Object |
94+ | | | - ` optional : boolean ` defaults to false |
95+ | | | - ` maxLength: [length: number, error: string] ` |
96+ | | | - ` minLength: [length: number, error: string] ` |
97+ | | | - ` pattern : [pattern: RegExp, error: string] ` |
98+ | | | |
99+ | number | ` number(options?) ` | options(optional): Object |
100+ | | | - ` optional: boolean ` default to false |
101+ | | | - ` min: [number, error: string] ` |
102+ | | | - ` max: [number, error: string] ` |
103+ | | | - `is : [ 'integer' | 'float', error: string] ` default is both |
104+ | | | |
105+ | boolean | ` boolean(options?) ` | options(optional): Object |
106+ | | | - ` optional: boolean ` default to false |
107+ | | | |
108+ | list | ` list(validators[], options?) ` | validators: Array of validators |
109+ | | | options(optional): Object |
110+ | | | - ` optional: boolean ` default to false |
111+ | | | |
112+ | listof | ` listof(validator, options?) ` | validator: Validator |
113+ | | | options(optional): Object |
114+ | | | - ` optional: boolean ` default to false |
115+ | | | |
116+ | record | ` record(shape, options?) ` | shape: ` Object { [key: string]: Validator } ` |
117+ | | | options(optional): Object |
118+ | | | - ` optional: boolean ` default to false |
119+ | | | |
120+ | recordof | ` recordof(validator, options?) ` | validator: ` Validator ` |
121+ | | | options(optional): Object |
122+ | | | - ` optional: boolean ` default to false |
122123
123124### Custom validators
124125
125- To create custom validators that does not break type inference:
126+ To create custom validators that do not break type inference:
126127
127128- use validators from ` _ ` as building blocks for your custom validator.
128- - your custom validator should define an ` optional ` and ` required ` functions.
129+ - your custom validator should define ` optional ` and ` required ` functions.
129130
130131Example of creating custom validators:
131132
@@ -149,12 +150,12 @@ const Person = createSchema({
149150
150151## Advanced usage
151152
152- In addition to validating data, you can also reuse your schema in other areas, like creating forms UI for example,
153+ In addition to validating data, you can also reuse your schema in other areas, like creating forms UI.
153154` traverse ` function come-in handy to help you achieve that.
154155
155156### Example
156157
157- In this example, we will transform a schema to create meta-data to be used to create form UI elements.
158+ In this example, we will transform a schema to create meta-data to create form UI elements.
158159
159160``` js
160161const User = createSchema ({
@@ -190,12 +191,11 @@ console.log(form_ui); /*
190191
191192### How to traverse
192193
193- The return type of your visitor is important, and there are a few considerations:
194-
195- Returning ` null ` from visitor signals to ignore this node from the result, with the exception:
196- ` record | recordof | list | listof ` , returning ` null ` signals to continue down recursively.
194+ there are a few considerations when defining ` visitor ` object:
197195
198- So to return something from ` record ` visitor for example, you will need to visit its children recursively.
196+ - In ` string | number | boolean ` visitors, returning ` null ` signals to ignore this node.
197+ - In ` record | recordof | list | listof ` visitors, returning ` null ` signals to visit its children.
198+ - To skip over ` record | recordof | list | listof ` nodes, return ` {} ` (empty object).
199199
200200Continuing from the previous ` User ` Example
201201
@@ -251,4 +251,4 @@ list.validate({ list: ['string', 42, 'string'] }); // { list: { 1: 'invalid-type
251251
252252## Recursive types
253253
254- Currently there's no easy way to create recursive types, if you think you could help, PRs are welcome
254+ Currently, there's no easy way to create recursive types. if you think you could help, PRs are welcome
0 commit comments