-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
42 lines (39 loc) · 1.05 KB
/
index.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
/**
* Copyright 2021 - Offen Authors <[email protected]>
* SPDX-License-Identifier: MPL-2.0
*/
const { validate, parse, serialize, explain } = require('./src')
const schemas = require('./schema')
module.exports = {
validate: validate,
mustValidate: throwingOnError(validate),
parse: parse,
mustParse: throwingOnError(parse),
serialize: serialize,
mustSerialize: throwingOnError(serialize),
explain: explain,
mustExplain: throwingOnError(explain),
defaultVersion: schemas.defaultVersion,
schema: schemas[schemas.defaultVersion]
}
/**
* Wraps an error returning function (either a single value or a result/error
* tuple) and returns a function of the same signature that will throw on error
* instead.
* @param {function} baseFn
* @returns {function}
*/
function throwingOnError (baseFn) {
return function () {
const result = baseFn.apply(null, [].slice.call(arguments))
if (Array.isArray(result)) {
if (result[1]) {
throw result[1]
}
return result[0]
}
if (result) {
throw result
}
}
}