UNPKG

8.33 kBMarkdownView Raw
1# PsychoType
2
3 [![license](https://img.shields.io/npm/l/psycho-type)](https://www.npmjs.com/package/psycho-type)
4 [![Version](https://img.shields.io/npm/v/psycho-type)](https://www.npmjs.com/package/psycho-type)
5 [![Size](https://packagephobia.now.sh/badge?p=psycho-type)](https://packagephobia.now.sh/result?p=psycho-type)
6
7 Simple and lightweight type validation library for [node](http://nodejs.org).
8
9```js
10const type = require('psycho-type')
11const referece = {
12 'foo': type.string(),
13 'bar': type.number()
14}
15
16type.check( reference, {
17 'foo': 'string',
18 'bar': 3
19})
20// => true
21```
22
23## Installation
24This is a [node.js](http://nodejs.org) module available via [npm](https://www.npmjs.com/).
25Requires node.js 4.9.1 or highter.
26
27```bash
28$ npm i psycho-type
29```
30
31## Usage
32This module can be used for runtime type validation for one item or (nested) object.
33
34```js
35/* Validating single item */
36const valid_type = type.string()
37
38/* You can do it with check function */
39type.check( valid_type, 'string' )
40//=> true
41
42/* Or only with valid type function */
43valid_type( 'string' )
44//=> true
45```
46
47Object validation is a little similar to [TypeScript](https://www.typescriptlang.org/) [Interfaces](https://www.typescriptlang.org/docs/handbook/interfaces.html).
48
49```js
50/* Validating objects */
51const reference = { // Reference object, contain types.
52 'foo': type.string(), //
53 'bar': type.boolean(), //
54 'fizz': { //
55 'buzz': type.number() //
56 }
57}
58
59const object = { // Object to validate.
60 'foo': 'String', //
61 'bar': true, //
62 'fizz': { //
63 'buzz': 15 //
64 }
65}
66
67type.check( reference, object )
68//=> true
69```
70
71# Types / Methods
72
73+ [Basic types](#Basic-Types)
74 - [Any](#Any)
75 - [String](#String)
76 - [Numbers](#Numbers)
77 * [Any Number](#Any-Number)
78 * [Number](#Number)
79 * [BigInt](#BigInt)
80 - [Boolean](#Boolean)
81 - [Function](#Function)
82 - [Object](#Object)
83 - [Array](#Array)
84 - [Symbol](#Symbol)
85 - [Undefined](#Undefined)
86 - [Null](#Null)
87 - [NaN](#NaN)
88+ [Complex types](#Complex-Types)
89 - [No Value](#No-Value)
90 - [Empty](#Empty)
91 - [Array Of _\<types\>_](#Array-Of-types)
92 - [Enum _\<values\>_](#Enum-values)
93 - [Some Of _\<types\>_](#Some-Of-types)
94 - [Not _\<types\>_](#Not-types)
95+ [Check Method](#Check-Method)
96
97## Basic Types
98
99### Any
100Just returns true on everything.
101
102 Param {Any} item
103 Return {Boolean}
104
105```js
106const any = type.any()
107
108any( * )
109//=> true
110```
111
112### String
113Allow both ' "string" ' and ' new String() '.
114
115 Param {Any} item
116 Return {Boolean}
117
118```js
119const string = type.string()
120
121string( 's' )
122//=> true
123
124string( 3 )
125//=> false
126
127string( new String() )
128//=> true
129```
130
131### Numbers
132
133#### Any Number
134Allow number, bigInt or NaN.
135
136 Param {Any} item
137 Return {Boolean}
138
139```js
140const anyNumber = type.anyNumber()
141
142anyNumber( 3 )
143//=> true
144
145anyNumber( new BigInt() )
146//=> true
147
148anyNumber( '3' )
149//=> false
150```
151
152#### Number
153Allow only regular number.
154
155 Param {Any} item
156 Return {Boolean}
157
158```js
159const number = type.number()
160
161number( 3 )
162//=> true
163
164number( new BigInt() )
165//=> false
166
167number( '3' )
168//=> false
169```
170
171#### BigInt
172Allow only BigInt.
173Have two aliases:
174 - bigInt
175 - bigNumber
176
177<!-- end of the list -->
178
179 Param {Any} item
180 Return {Boolean}
181
182```js
183const bigint = type.bigInt()
184
185bigint( 3 )
186//=> false
187
188bigint( new BigInt() )
189//=> true
190
191bigint( '3n' )
192//=> false
193```
194
195### Boolean
196Allow only boolean.
197Have two aliases:
198 - bool
199 - boolean
200
201<!-- end of the list -->
202
203 Param {Any} item
204 Return {Boolean}
205
206```js
207const bool = type.bool()
208
209bool( 3 )
210//=> false
211
212bool( true )
213//=> true
214
215bool( new Boolean(1) )
216//=> true
217```
218
219### Function
220Allow only functions.
221
222 ! Any class/constructor will return true !
223
224 Param {Any} item
225 Return {Boolean}
226
227```js
228const func = type.function()
229
230func( 3 )
231//=> false
232
233func( () => {} )
234//=> true
235
236func( Boolean ) // Because ' Boolean ' it's the constructor
237//=> true
238```
239
240### Object
241Allow only objects.
242
243 ! Doesn't allow null or basic data types even if it's created by ' new Object() ' !
244
245 Param {Any} item
246 Return {Boolean}
247
248```js
249const object = type.object()
250
251object( new Object() )
252//=> true
253
254object( new Object(3) )
255//=> false
256
257object( {} )
258//=> true
259```
260
261### Array
262Allow array with any value.
263
264 Param {Any} item
265 Return {Boolean}
266
267```js
268const array = type.array()
269
270array( [] )
271//=> true
272
273array( new array( 3, 4 ) )
274//=> true
275
276array( {} )
277//=> false
278```
279
280### Symbol
281Allow symbol.
282
283 Param {Any} item
284 Return {Boolean}
285
286```js
287const symbol = type.symbol()
288
289symbol( [] )
290//=> false
291
292symbol( Symbol( '3' ) )
293//=> true
294
295symbol( {} )
296//=> false
297```
298
299### Undefined
300Allow only undefined.
301
302 Param {Any} item
303 Return {Boolean}
304
305```js
306const not_defined = type.undefined()
307
308not_defined( [] )
309//=> false
310
311not_defined( null )
312//=> false
313
314not_defined( undefined )
315//=> true
316```
317
318### Null
319Allow only null.
320
321 Param {Any} item
322 Return {Boolean}
323
324```js
325const null_type = type.null()
326
327null_type( [] )
328//=> false
329
330null_type( null )
331//=> true
332
333null_type( undefined )
334//=> false
335```
336
337### NaN
338Allow only NaN.
339Have two aliases:
340 - nan
341 - NaN
342
343<!-- end of the list -->
344
345 Param {Any} item
346 Return {Boolean}
347
348```js
349const null_type = type.null()
350
351null_type( [] )
352//=> false
353
354null_type( null )
355//=> true
356
357null_type( undefined )
358//=> false
359```
360
361## Complex Types
362
363### No Value
364Allow ' no value ' types such as undefined, null, NaN.
365
366 Param {Any} item
367 Return {Boolean}
368
369```js
370const no_value = type.noValue()
371
372no_value( [] )
373//=> false
374
375no_value( null )
376//=> true
377
378no_value( undefined )
379//=> true
380```
381
382### Empty
383Allow ' empty ' values such as {}, [], '' and 0.
384
385 ! Doesn't work NOW with values created from constructor ( like ' new String() ' ) !
386
387 Param {Any} item
388 Return {Boolean}
389
390```js
391const empty = type.empty()
392
393empty( [] )
394//=> true
395
396empty( null )
397//=> false
398
399empty( new String() )
400//=> false
401```
402
403### Array Of _\<types\>_
404Allow array with some types.
405
406 Param {Array} ...types
407 Return {function}:
408 Param {Array} item
409 Return {Boolean}
410
411```js
412const array = type.arrayOf(
413 type.string(),
414 type.boolean()
415 )
416
417array([ 'string', true ])
418//=> true
419
420array( null )
421//=> false
422
423array( new Boolean() )
424//=> true
425```
426
427### Enum _\<values\>_
428Allow only some values.
429
430 ! Doesn't work NOW for values, created by like ' new Number() ' !
431
432 Param {Array} ...values
433 Return {function}:
434 Param {Any} item
435 Return {Boolean}
436
437```js
438const enum = type.enum(
439 'value',
440 3
441 )
442
443enum( 'value' )
444//=> true
445
446enum( 3 )
447//=> true
448
449enum( 4 )
450//=> false
451```
452
453### Some Of _\<types\>_
454Allow some types.
455
456 Param {Array} ...types
457 Return {function}:
458 Param {Any} item
459 Return {Boolean}
460
461```js
462const some = type.someOf(
463 type.string(),
464 type.boolean()
465 )
466
467some( 'value' )
468//=> true
469
470some( 3 )
471//=> false
472
473some( false )
474//=> true
475```
476
477### Not _\<types\>_
478Inverted ' someOf '. Disallow some types.
479
480 Param {Array} ...types
481 Return {function}:
482 Param {Any} item
483 Return {Boolean}
484
485```js
486const not = type.not(
487 type.string(),
488 type.boolean()
489 )
490
491some( 'value' )
492//=> false
493
494some( 3 )
495//=> true
496
497some( false )
498//=> false
499```
500## Check Method
501Compare types of input object with reference.
502
503 ! Method will return false if reference is not valid, without any exeption !
504
505 Param {Object|Function} reference/type
506 Return {Boolean}
507
508```js
509const reference = { // Reference object, contain types.
510 'foo': type.string(), //
511 'bar': type.boolean(), //
512 'fizz': { //
513 'buzz': type.number() //
514 }
515}
516
517const object = { // Valid object.
518 'foo': 'String', //
519 'bar': true, //
520 'fizz': { //
521 'buzz': 15 //
522 }
523}
524
525type.check( reference, object )
526//=> true
527```
528
529# Contacts
530If you have any questions or suggestions please contact me:
531 <dmitry.kokhanevich@gmail.com>