UNPKG

4.84 kBJavaScriptView Raw
1"use strict";
2var __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};
13Object.defineProperty(exports, "__esModule", { value: true });
14var TYPE_ERROR = 'type_error_expected';
15function 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}
25function 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}
34function matchesBoolean(options) {
35 return function (value) {
36 return typeof value === 'boolean'
37 ? undefined
38 : constructErrors(value, TYPE_ERROR + "_boolean", options);
39 };
40}
41exports.matchesBoolean = matchesBoolean;
42function matchesString(options) {
43 return function (value) {
44 return typeof value === 'string' ? undefined : constructErrors(value, TYPE_ERROR + "_string", options);
45 };
46}
47exports.matchesString = matchesString;
48function matchesPositiveInteger(options) {
49 return function (value) {
50 return !Number.isInteger(value) || value < 0
51 ? constructErrors(value, TYPE_ERROR + "_integer", options)
52 : undefined;
53 };
54}
55exports.matchesPositiveInteger = matchesPositiveInteger;
56function 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}
63exports.matchesPositiveNumber = matchesPositiveNumber;
64/**
65 * Returns a validator that matches values in the given enum
66 * @param type - enum to use for match values
67 * @public
68 */
69function 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}
78exports.matchesEnum = matchesEnum;
79function 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}
97exports.matchesArray = matchesArray;
98function 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}
109exports.matchesObject = matchesObject;
110function makeOptional(validator) {
111 return function (value) {
112 if (value === undefined || value === null) {
113 return undefined;
114 }
115 return validator(value);
116 };
117}
118exports.makeOptional = makeOptional;
119function 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}
137exports.composeSchemas = composeSchemas;
138function validate(obj, validator) {
139 return getErrors(obj, validator);
140}
141exports.validate = validate;