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