deno.land / x / rambda@v9.1.1 / source / ok.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
import { any } from './any.js'import { glue } from './glue.js'import { fromPrototypeToString, isValid } from './isValid.js'import { map } from './map.js'import { type } from './type.js'
export function schemaToString(schema){ if (type(schema) !== 'Object'){ return fromPrototypeToString(schema).rule }
return map(x => { const { rule, parsed } = fromPrototypeToString(x) const xType = type(x)
if (xType === 'Function' && !parsed) return 'Function'
return parsed ? rule : xType }, schema)}
export function check(singleInput, schema){ return isValid({ input : { singleInput }, schema : { singleInput : schema }, })}
export function ok(...inputs){ return (...schemas) => { let failedSchema
const anyError = any((singleInput, i) => { const schema = schemas[ i ] === undefined ? schemas[ 0 ] : schemas[ i ]
const checked = check(singleInput, schema) if (!checked){ failedSchema = JSON.stringify({ input : singleInput, schema : schemaToString(schema), }) }
return !checked }, inputs)
if (anyError){ const errorMessage = inputs.length > 1 ? glue(` Failed R.ok - reason: ${ failedSchema } all inputs: ${ JSON.stringify(inputs) } all schemas: ${ JSON.stringify(schemas.map(schemaToString)) } `, '\n') : `Failed R.ok - ${ failedSchema }`
throw new Error(errorMessage) } }}
rambda

Version Info

Tagged at
2 months ago