source: trip-planner-front/node_modules/aproba/index.js@ 188ee53

Last change on this file since 188ee53 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.9 KB
Line 
1'use strict'
2
3function isArguments (thingy) {
4 return thingy != null && typeof thingy === 'object' && thingy.hasOwnProperty('callee')
5}
6
7var types = {
8 '*': {label: 'any', check: function () { return true }},
9 A: {label: 'array', check: function (thingy) { return Array.isArray(thingy) || isArguments(thingy) }},
10 S: {label: 'string', check: function (thingy) { return typeof thingy === 'string' }},
11 N: {label: 'number', check: function (thingy) { return typeof thingy === 'number' }},
12 F: {label: 'function', check: function (thingy) { return typeof thingy === 'function' }},
13 O: {label: 'object', check: function (thingy) { return typeof thingy === 'object' && thingy != null && !types.A.check(thingy) && !types.E.check(thingy) }},
14 B: {label: 'boolean', check: function (thingy) { return typeof thingy === 'boolean' }},
15 E: {label: 'error', check: function (thingy) { return thingy instanceof Error }},
16 Z: {label: 'null', check: function (thingy) { return thingy == null }}
17}
18
19function addSchema (schema, arity) {
20 var group = arity[schema.length] = arity[schema.length] || []
21 if (group.indexOf(schema) === -1) group.push(schema)
22}
23
24var validate = module.exports = function (rawSchemas, args) {
25 if (arguments.length !== 2) throw wrongNumberOfArgs(['SA'], arguments.length)
26 if (!rawSchemas) throw missingRequiredArg(0, 'rawSchemas')
27 if (!args) throw missingRequiredArg(1, 'args')
28 if (!types.S.check(rawSchemas)) throw invalidType(0, ['string'], rawSchemas)
29 if (!types.A.check(args)) throw invalidType(1, ['array'], args)
30 var schemas = rawSchemas.split('|')
31 var arity = {}
32
33 schemas.forEach(function (schema) {
34 for (var ii = 0; ii < schema.length; ++ii) {
35 var type = schema[ii]
36 if (!types[type]) throw unknownType(ii, type)
37 }
38 if (/E.*E/.test(schema)) throw moreThanOneError(schema)
39 addSchema(schema, arity)
40 if (/E/.test(schema)) {
41 addSchema(schema.replace(/E.*$/, 'E'), arity)
42 addSchema(schema.replace(/E/, 'Z'), arity)
43 if (schema.length === 1) addSchema('', arity)
44 }
45 })
46 var matching = arity[args.length]
47 if (!matching) {
48 throw wrongNumberOfArgs(Object.keys(arity), args.length)
49 }
50 for (var ii = 0; ii < args.length; ++ii) {
51 var newMatching = matching.filter(function (schema) {
52 var type = schema[ii]
53 var typeCheck = types[type].check
54 return typeCheck(args[ii])
55 })
56 if (!newMatching.length) {
57 var labels = matching.map(function (schema) {
58 return types[schema[ii]].label
59 }).filter(function (schema) { return schema != null })
60 throw invalidType(ii, labels, args[ii])
61 }
62 matching = newMatching
63 }
64}
65
66function missingRequiredArg (num) {
67 return newException('EMISSINGARG', 'Missing required argument #' + (num + 1))
68}
69
70function unknownType (num, type) {
71 return newException('EUNKNOWNTYPE', 'Unknown type ' + type + ' in argument #' + (num + 1))
72}
73
74function invalidType (num, expectedTypes, value) {
75 var valueType
76 Object.keys(types).forEach(function (typeCode) {
77 if (types[typeCode].check(value)) valueType = types[typeCode].label
78 })
79 return newException('EINVALIDTYPE', 'Argument #' + (num + 1) + ': Expected ' +
80 englishList(expectedTypes) + ' but got ' + valueType)
81}
82
83function englishList (list) {
84 return list.join(', ').replace(/, ([^,]+)$/, ' or $1')
85}
86
87function wrongNumberOfArgs (expected, got) {
88 var english = englishList(expected)
89 var args = expected.every(function (ex) { return ex.length === 1 })
90 ? 'argument'
91 : 'arguments'
92 return newException('EWRONGARGCOUNT', 'Expected ' + english + ' ' + args + ' but got ' + got)
93}
94
95function moreThanOneError (schema) {
96 return newException('ETOOMANYERRORTYPES',
97 'Only one error type per argument signature is allowed, more than one found in "' + schema + '"')
98}
99
100function newException (code, msg) {
101 var e = new Error(msg)
102 e.code = code
103 if (Error.captureStackTrace) Error.captureStackTrace(e, validate)
104 return e
105}
Note: See TracBrowser for help on using the repository browser.