UNPKG

8.66 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+ [Of Method](#Of-Method)
97
98## Basic Types
99
100### Any
101Just returns true on everything.
102
103 Param {Any} item
104 Return {Boolean}
105
106```js
107const any = type.any()
108
109any( * )
110//=> true
111```
112
113### String
114Allow both ' "string" ' and ' new String() '.
115
116 Param {Any} item
117 Return {Boolean}
118
119```js
120const string = type.string()
121
122string( 's' )
123//=> true
124
125string( 3 )
126//=> false
127
128string( new String() )
129//=> true
130```
131
132### Numbers
133
134#### Any Number
135Allow number, bigInt or NaN.
136
137 Param {Any} item
138 Return {Boolean}
139
140```js
141const anyNumber = type.anyNumber()
142
143anyNumber( 3 )
144//=> true
145
146anyNumber( BigInt() )
147//=> true
148
149anyNumber( '3' )
150//=> false
151```
152
153#### Number
154Allow only regular number.
155
156 Param {Any} item
157 Return {Boolean}
158
159```js
160const number = type.number()
161
162number( 3 )
163//=> true
164
165number( BigInt() )
166//=> false
167
168number( '3' )
169//=> false
170```
171
172#### BigInt
173Allow only BigInt.
174Have two aliases:
175 - bigInt
176 - bigNumber
177
178<!-- end of the list -->
179
180 Param {Any} item
181 Return {Boolean}
182
183```js
184const bigint = type.bigInt()
185
186bigint( 3 )
187//=> false
188
189bigint( BigInt() )
190//=> true
191
192bigint( '3n' )
193//=> false
194```
195
196### Boolean
197Allow only boolean.
198Have two aliases:
199 - bool
200 - boolean
201
202<!-- end of the list -->
203
204 Param {Any} item
205 Return {Boolean}
206
207```js
208const bool = type.bool()
209
210bool( 3 )
211//=> false
212
213bool( true )
214//=> true
215
216bool( new Boolean(1) )
217//=> true
218```
219
220### Function
221Allow only functions.
222
223 ! Any class/constructor will return true !
224
225 Param {Any} item
226 Return {Boolean}
227
228```js
229const func = type.function()
230
231func( 3 )
232//=> false
233
234func( () => {} )
235//=> true
236
237func( Boolean ) // Because ' Boolean ' it's the constructor
238//=> true
239```
240
241### Object
242Allow only objects.
243
244 ! Doesn't allow null or basic data types even if it's created by ' new Object() ' !
245
246 Param {Any} item
247 Return {Boolean}
248
249```js
250const object = type.object()
251
252object( new Object() )
253//=> true
254
255object( new Object(3) )
256//=> false
257
258object( {} )
259//=> true
260```
261
262### Array
263Allow array with any value.
264
265 Param {Any} item
266 Return {Boolean}
267
268```js
269const array = type.array()
270
271array( [] )
272//=> true
273
274array( new array( 3, 4 ) )
275//=> true
276
277array( {} )
278//=> false
279```
280
281### Symbol
282Allow symbol.
283
284 Param {Any} item
285 Return {Boolean}
286
287```js
288const symbol = type.symbol()
289
290symbol( [] )
291//=> false
292
293symbol( Symbol( '3' ) )
294//=> true
295
296symbol( {} )
297//=> false
298```
299
300### Undefined
301Allow only undefined.
302
303 Param {Any} item
304 Return {Boolean}
305
306```js
307const not_defined = type.undefined()
308
309not_defined( [] )
310//=> false
311
312not_defined( null )
313//=> false
314
315not_defined( undefined )
316//=> true
317```
318
319### Null
320Allow only null.
321
322 Param {Any} item
323 Return {Boolean}
324
325```js
326const null_type = type.null()
327
328null_type( [] )
329//=> false
330
331null_type( null )
332//=> true
333
334null_type( undefined )
335//=> false
336```
337
338### NaN
339Allow only NaN.
340Have two aliases:
341 - nan
342 - NaN
343
344<!-- end of the list -->
345
346 Param {Any} item
347 Return {Boolean}
348
349```js
350const null_type = type.null()
351
352null_type( [] )
353//=> false
354
355null_type( null )
356//=> true
357
358null_type( undefined )
359//=> false
360```
361
362## Complex Types
363
364### No Value
365Allow ' no value ' types such as undefined, null, NaN.
366
367 Param {Any} item
368 Return {Boolean}
369
370```js
371const no_value = type.noValue()
372
373no_value( [] )
374//=> false
375
376no_value( null )
377//=> true
378
379no_value( undefined )
380//=> true
381```
382
383### Empty
384Allow ' empty ' values such as {}, [], '' and 0.
385
386 ! Doesn't work NOW with values created from constructor ( like ' new String() ' ) !
387
388 Param {Any} item
389 Return {Boolean}
390
391```js
392const empty = type.empty()
393
394empty( [] )
395//=> true
396
397empty( null )
398//=> false
399
400empty( new String() )
401//=> false
402```
403
404### Array Of _\<types\>_
405Allow array with some types.
406
407 Param {Array} ...types
408 Return {function}:
409 Param {Array} item
410 Return {Boolean}
411
412```js
413const array = type.arrayOf(
414 type.string(),
415 type.boolean()
416 )
417
418array([ 'string', true ])
419//=> true
420
421array( null )
422//=> false
423
424array( new Boolean() )
425//=> true
426```
427
428### Enum _\<values\>_
429Allow only some values.
430
431 ! Doesn't work NOW for values, created by like ' new Number() ' !
432
433 Param {Array} ...values
434 Return {function}:
435 Param {Any} item
436 Return {Boolean}
437
438```js
439const enum = type.enum(
440 'value',
441 3
442 )
443
444enum( 'value' )
445//=> true
446
447enum( 3 )
448//=> true
449
450enum( 4 )
451//=> false
452```
453
454### Some Of _\<types\>_
455Allow some types.
456
457 Param {Array} ...types
458 Return {function}:
459 Param {Any} item
460 Return {Boolean}
461
462```js
463const some = type.someOf(
464 type.string(),
465 type.boolean()
466 )
467
468some( 'value' )
469//=> true
470
471some( 3 )
472//=> false
473
474some( false )
475//=> true
476```
477
478### Not _\<types\>_
479Inverted ' someOf '. Disallow some types.
480
481 Param {Array} ...types
482 Return {function}:
483 Param {Any} item
484 Return {Boolean}
485
486```js
487const not = type.not(
488 type.string(),
489 type.boolean()
490 )
491
492some( 'value' )
493//=> false
494
495some( 3 )
496//=> true
497
498some( false )
499//=> false
500```
501
502## 'Check' Method
503Compare types of input object with reference.
504
505 ! Method will return false if reference is not valid, without any exeption !
506
507 Param {Object|Function} reference/type
508 Return {Boolean}
509
510```js
511const reference = { // Reference object, contain types.
512 'foo': type.string(), //
513 'bar': type.boolean(), //
514 'fizz': { //
515 'buzz': type.number() //
516 }
517}
518
519const object = { // Valid object.
520 'foo': 'String', //
521 'bar': true, //
522 'fizz': { //
523 'buzz': 15 //
524 }
525}
526
527type.check( reference, object )
528//=> true
529```
530
531## 'Of' Method
532Returns type of input even if input created like ' new Object(3) ' and
533has custom types: 'null', 'NaN'.
534
535 Param {Any} item
536 Return {String}
537
538```js
539type.of( Number );
540//=> 'function'
541
542type.of( null );
543//=> 'null'
544
545type.of( NaN );
546//=> 'NaN'
547
548type.of( new Object('string') );
549//=> 'string'
550```
551
552# Contacts
553If you have any questions or suggestions please contact me:
554 <dmitry.kokhanevich@gmail.com>