1 | "use strict";
|
2 | var __assign = (this && this.__assign) || function () {
|
3 | __assign = Object.assign || function(t) {
|
4 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
5 | s = arguments[i];
|
6 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
7 | t[p] = s[p];
|
8 | }
|
9 | return t;
|
10 | };
|
11 | return __assign.apply(this, arguments);
|
12 | };
|
13 | Object.defineProperty(exports, "__esModule", { value: true });
|
14 | var TYPE_ERROR = 'type_error_expected';
|
15 | function constructErrors(value, error, options) {
|
16 | if (options === void 0) { options = { message: undefined }; }
|
17 | return [
|
18 | {
|
19 | value: value,
|
20 | error: error,
|
21 | message: typeof options.message === 'function' ? options.message(error, value) : options.message,
|
22 | },
|
23 | ];
|
24 | }
|
25 | function getErrors(obj, validator, key) {
|
26 | var value = key ? obj[key] : obj;
|
27 | var path = key ? "['" + key + "']" : undefined;
|
28 | var error = validator(value);
|
29 | if (!error) {
|
30 | return;
|
31 | }
|
32 | return error.map(function (o) { return (__assign({}, o, { path: "" + (path || '') + (o.path || '') || undefined })); });
|
33 | }
|
34 | function matchesBoolean(options) {
|
35 | return function (value) {
|
36 | return typeof value === 'boolean'
|
37 | ? undefined
|
38 | : constructErrors(value, TYPE_ERROR + "_boolean", options);
|
39 | };
|
40 | }
|
41 | exports.matchesBoolean = matchesBoolean;
|
42 | function matchesString(options) {
|
43 | return function (value) {
|
44 | return typeof value === 'string' ? undefined : constructErrors(value, TYPE_ERROR + "_string", options);
|
45 | };
|
46 | }
|
47 | exports.matchesString = matchesString;
|
48 | function matchesPositiveInteger(options) {
|
49 | return function (value) {
|
50 | return !Number.isInteger(value) || value < 0
|
51 | ? constructErrors(value, TYPE_ERROR + "_integer", options)
|
52 | : undefined;
|
53 | };
|
54 | }
|
55 | exports.matchesPositiveInteger = matchesPositiveInteger;
|
56 | function matchesPositiveNumber(options) {
|
57 | return function (value) {
|
58 | return Number.isNaN(value) || !Number.isFinite(value) || value < 0
|
59 | ? constructErrors(value, TYPE_ERROR + "_number", options)
|
60 | : undefined;
|
61 | };
|
62 | }
|
63 | exports.matchesPositiveNumber = matchesPositiveNumber;
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | function matchesEnum(types, options) {
|
70 | return function (value) {
|
71 | var values = Object.keys(types).map(function (key) { return types[key]; });
|
72 | var message = (options && options.message) || "expected:" + values.map(function (val) { return "`" + val + "`"; }).join(' or ');
|
73 | return values.includes(value)
|
74 | ? undefined
|
75 | : constructErrors(value, 'invalid_enum_value', __assign({}, options, { message: message }));
|
76 | };
|
77 | }
|
78 | exports.matchesEnum = matchesEnum;
|
79 | function matchesArray(validator, options) {
|
80 | return function (value) {
|
81 | if (!Array.isArray(value)) {
|
82 | return constructErrors(value, TYPE_ERROR + "_array", options);
|
83 | }
|
84 | if (!validator) {
|
85 | return;
|
86 | }
|
87 | var errors = [];
|
88 | value.forEach(function (val, key) {
|
89 | var objectError = validator(val);
|
90 | if (objectError) {
|
91 | errors = errors.concat(objectError.map(function (error) { return (__assign({}, error, { path: "['" + key + "']" + (error.path || '') })); }));
|
92 | }
|
93 | });
|
94 | return errors.length ? errors : undefined;
|
95 | };
|
96 | }
|
97 | exports.matchesArray = matchesArray;
|
98 | function matchesObject(schema, options) {
|
99 | return function (val) {
|
100 | if (typeof val !== 'object' || !val || Array.isArray(val)) {
|
101 | return constructErrors(val, TYPE_ERROR + "_object", options);
|
102 | }
|
103 | var flattened = Object.keys(schema).reduce(function (acc, key) {
|
104 | return acc.concat((getErrors(val, schema[key], key) || []));
|
105 | }, []);
|
106 | return flattened.length ? flattened : undefined;
|
107 | };
|
108 | }
|
109 | exports.matchesObject = matchesObject;
|
110 | function makeOptional(validator) {
|
111 | return function (value) {
|
112 | if (value === undefined || value === null) {
|
113 | return undefined;
|
114 | }
|
115 | return validator(value);
|
116 | };
|
117 | }
|
118 | exports.makeOptional = makeOptional;
|
119 | function composeSchemas() {
|
120 | var validators = [];
|
121 | for (var _i = 0; _i < arguments.length; _i++) {
|
122 | validators[_i] = arguments[_i];
|
123 | }
|
124 | return function (val) {
|
125 | var error;
|
126 | var i = 0;
|
127 | var len = validators.length;
|
128 | while (!error && i < len) {
|
129 | error = validators[i](val);
|
130 | if (error) {
|
131 | return error;
|
132 | }
|
133 | i++;
|
134 | }
|
135 | };
|
136 | }
|
137 | exports.composeSchemas = composeSchemas;
|
138 | function validate(obj, validator) {
|
139 | return getErrors(obj, validator);
|
140 | }
|
141 | exports.validate = validate;
|