1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
10 | exports.convert = exports.undefinedIfInvalid = exports.isValid = exports.getValidationResult = exports.validate = void 0;
|
11 | const js_lib_1 = require("@naturalcycles/js-lib");
|
12 | const joi_validation_error_1 = require("./joi.validation.error");
|
13 |
|
14 |
|
15 |
|
16 | const stripColors = true;
|
17 | const defaultOptions = {
|
18 | abortEarly: false,
|
19 | convert: true,
|
20 | allowUnknown: true,
|
21 | stripUnknown: {
|
22 | objects: true,
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | arrays: false,
|
30 | },
|
31 | presence: 'required',
|
32 |
|
33 |
|
34 |
|
35 | };
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | function validate(value, schema, objectName, options = {}) {
|
44 | const { value: returnValue, error } = getValidationResult(value, schema, objectName, options);
|
45 | if (error) {
|
46 | throw error;
|
47 | }
|
48 | return returnValue;
|
49 | }
|
50 | exports.validate = validate;
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | function getValidationResult(value, schema, objectName, options = {}) {
|
59 | if (!schema)
|
60 | return { value };
|
61 | const { value: returnValue, error } = schema.validate(value, {
|
62 | ...defaultOptions,
|
63 | ...options,
|
64 | });
|
65 | const vr = {
|
66 | value: returnValue,
|
67 | };
|
68 | if (error) {
|
69 | vr.error = createError(value, error, objectName);
|
70 | }
|
71 | return vr;
|
72 | }
|
73 | exports.getValidationResult = getValidationResult;
|
74 |
|
75 |
|
76 |
|
77 | function isValid(value, schema) {
|
78 | if (!schema)
|
79 | return { value };
|
80 | const { error } = schema.validate(value, defaultOptions);
|
81 | return !error;
|
82 | }
|
83 | exports.isValid = isValid;
|
84 | function undefinedIfInvalid(value, schema) {
|
85 | if (!schema)
|
86 | return { value };
|
87 | const { value: returnValue, error } = schema.validate(value, defaultOptions);
|
88 | return error ? undefined : returnValue;
|
89 | }
|
90 | exports.undefinedIfInvalid = undefinedIfInvalid;
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 | function convert(value, schema) {
|
97 | if (!schema)
|
98 | return value;
|
99 | const { value: returnValue } = schema.validate(value, defaultOptions);
|
100 | return returnValue;
|
101 | }
|
102 | exports.convert = convert;
|
103 | function createError(value, err, objectName) {
|
104 | if (!err)
|
105 | return undefined;
|
106 | const tokens = [];
|
107 | const objectId = (0, js_lib_1._isObject)(value) ? value['id'] : undefined;
|
108 | if (objectId || objectName) {
|
109 | objectName = objectName || value?.constructor?.name;
|
110 | tokens.push('Invalid ' + [objectName, objectId].filter(Boolean).join('.'));
|
111 | }
|
112 | const annotation = err.annotate(stripColors);
|
113 | if (annotation.length > 4000) {
|
114 |
|
115 | tokens.push((0, js_lib_1._truncateMiddle)(annotation, 4000, `\n... ${(0, js_lib_1._hb)(annotation.length)} message truncated ...\n`));
|
116 |
|
117 | tokens.push(...err.details.slice(0, 5).map(i => `${i.message} @ .${i.path.join('.')}`));
|
118 | if (err.details.length > 5)
|
119 | tokens.push(`... ${err.details.length} errors`);
|
120 | }
|
121 | else {
|
122 | tokens.push(annotation);
|
123 | }
|
124 | const msg = tokens.join('\n');
|
125 | const data = {
|
126 | joiValidationErrorItems: err.details,
|
127 | ...(objectName && { joiValidationObjectName: objectName }),
|
128 | ...(objectId && { joiValidationObjectId: objectId }),
|
129 | };
|
130 |
|
131 |
|
132 | Object.defineProperty(data, 'annotation', {
|
133 | writable: true,
|
134 | configurable: true,
|
135 | enumerable: false,
|
136 | value: annotation,
|
137 | });
|
138 | return new joi_validation_error_1.JoiValidationError(msg, data);
|
139 | }
|