deno.land / x / rambda@v9.1.1 / source / isValid.js

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { isArray } from './_internals/isArray.js'import { all } from './all.js'import { any } from './any.js'import { includes } from './includes.js'import { init } from './init.js'import { test } from './test.js'import { toLower } from './toLower.js'import { type } from './type.js'
export function isPrototype(input){ const currentPrototype = input.prototype const list = [ Number, String, Boolean, Promise ] let toReturn = false let counter = -1 while (++counter < list.length && !toReturn){ if (currentPrototype === list[ counter ].prototype) toReturn = true }
return toReturn}
export function prototypeToString(input){ const currentPrototype = input.prototype const list = [ Number, String, Boolean, Promise ] const translatedList = [ 'Number', 'String', 'Boolean', 'Promise' ] let found let counter = -1
while (++counter < list.length){ if (currentPrototype === list[ counter ].prototype) found = counter }
return translatedList[ found ]}
const typesWithoutPrototype = [ 'any', 'promise', 'async', 'function' ]
export function fromPrototypeToString(rule){ if ( isArray(rule) || rule === undefined || rule === null || rule.prototype === undefined || typesWithoutPrototype.includes(rule) ){ return { rule, parsed : false, } } if (String.prototype === rule.prototype){ return { rule : 'string', parsed : true, } } if (Boolean.prototype === rule.prototype){ return { rule : 'boolean', parsed : true, } } if (Number.prototype === rule.prototype){ return { rule : 'number', parsed : true, } }
return { rule : type(rule.prototype).toLowerCase(), parsed : true, }}
function getRuleAndType(schema, requirementRaw){ const ruleRaw = schema[ requirementRaw ] const typeIs = type(ruleRaw) const { rule, parsed } = fromPrototypeToString(ruleRaw)
return { rule, ruleType : parsed ? 'String' : typeIs, }}
export function isValid({ input, schema }){ if (input === undefined || schema === undefined) return false
let flag = true const boom = boomFlag => { if (!boomFlag){ flag = false } }
for (const requirementRaw in schema){ if (flag){ const isOptional = requirementRaw.endsWith('?') const requirement = isOptional ? init(requirementRaw) : requirementRaw
const { rule, ruleType } = getRuleAndType(schema, requirementRaw) const inputProp = input[ requirement ] const inputPropType = type(input[ requirement ])
const ok = isOptional && inputProp !== undefined || !isOptional
if (!ok || rule === 'any' && inputProp != null || rule === inputProp) continue
if (ruleType === 'Object'){ /** * This rule is standalone schema, so we recursevly call `isValid` */ const isValidResult = isValid({ input : inputProp, schema : rule, }) boom(isValidResult) } else if (ruleType === 'String'){ /** * Rule is actual rule such as 'number', so the two types are compared */ boom(toLower(inputPropType) === rule) } else if (typeof rule === 'function'){ /** * Rule is function so we pass to it the input */ boom(rule(inputProp)) } else if (ruleType === 'Array' && inputPropType === 'String'){ /** * Enum case | rule is like a: ['foo', 'bar'] */ boom(includes(inputProp, rule)) } else if ( ruleType === 'Array' && rule.length === 1 && inputPropType === 'Array' ){ /** * 1. array of type | rule is like a: ['number'] * 2. rule is like a: [{foo: 'string', bar: 'number'}] */ const [ currentRule ] = rule const currentRuleType = type(currentRule)
//Check if rule is invalid boom(currentRuleType === 'String' || currentRuleType === 'Object' || isPrototype(currentRule))
if (currentRuleType === 'Object' && flag){ /** * 2. rule is like a: [{from: 'string'}] */ const isValidResult = all(inputPropInstance => isValid({ input : inputPropInstance, schema : currentRule, }), inputProp) boom(isValidResult) } else if (flag){ /** * 1. array of type */
const actualRule = currentRuleType === 'String' ? currentRule : prototypeToString(currentRule) const isInvalidResult = any(inputPropInstance => type(inputPropInstance).toLowerCase() !== actualRule.toLowerCase(), inputProp) boom(!isInvalidResult) } } else if (ruleType === 'RegExp' && inputPropType === 'String'){ boom(test(rule, inputProp)) } else { boom(false) } } }
return flag}
rambda

Version Info

Tagged at
2 months ago