1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.validateNested = validateNested;
|
7 | exports.validateList = validateList;
|
8 | exports.validateWithFields = validateWithFields;
|
9 | exports.validate = validate;
|
10 | exports.validateRequired = validateRequired;
|
11 | exports["default"] = void 0;
|
12 |
|
13 | var _predicates = require("@shopify/predicates");
|
14 |
|
15 | var _utilities = require("./utilities");
|
16 |
|
17 | function validateNested(validatorDictionary) {
|
18 | return function (input, fields) {
|
19 | var errors = (0, _utilities.mapObject)(input, function (value, field) {
|
20 | var validate = validatorDictionary[field];
|
21 |
|
22 | if (validate == null) {
|
23 | return null;
|
24 | }
|
25 |
|
26 | if (typeof validate === 'function') {
|
27 | return validate(value, fields);
|
28 | }
|
29 |
|
30 | if (!Array.isArray(validate)) {
|
31 | return;
|
32 | }
|
33 |
|
34 | var errors = validate.map(function (validator) {
|
35 | return validator(value, fields);
|
36 | }).filter(function (input) {
|
37 | return input != null;
|
38 | });
|
39 |
|
40 | if (errors.length === 0) {
|
41 | return;
|
42 | }
|
43 |
|
44 | return errors;
|
45 | });
|
46 | var anyErrors = Object.keys(errors).map(function (key) {
|
47 | return errors[key];
|
48 | }).some(function (value) {
|
49 | return value != null;
|
50 | });
|
51 |
|
52 | if (anyErrors) {
|
53 | return errors;
|
54 | }
|
55 | };
|
56 | }
|
57 |
|
58 | function validateList(validatorDictionary) {
|
59 | var validateItem = validateNested(validatorDictionary);
|
60 | return function (input, fields) {
|
61 | var errors = input.map(function (item) {
|
62 | return validateItem(item, fields);
|
63 | });
|
64 |
|
65 | if (errors.some(function (error) {
|
66 | return error != null;
|
67 | })) {
|
68 | return errors;
|
69 | }
|
70 | };
|
71 | }
|
72 |
|
73 | function validateWithFields(matcher, errorContent) {
|
74 | return validate(matcher, errorContent);
|
75 | }
|
76 |
|
77 | function validate(matcher, errorContent) {
|
78 | return function (input, fields) {
|
79 | var matches = matcher(input, fields);
|
80 | |
81 |
|
82 |
|
83 |
|
84 |
|
85 | if ((0, _predicates.isEmpty)(input)) {
|
86 | return;
|
87 | }
|
88 |
|
89 | if (matches) {
|
90 | return;
|
91 | }
|
92 |
|
93 | if (typeof errorContent === 'function') {
|
94 | return errorContent(toString(input));
|
95 | }
|
96 |
|
97 | return errorContent;
|
98 | };
|
99 | }
|
100 |
|
101 | function validateRequired(matcher, errorContent) {
|
102 | return function (input, fields) {
|
103 | var matches = matcher(input, fields);
|
104 |
|
105 | if (matches) {
|
106 | return;
|
107 | }
|
108 |
|
109 | if (typeof errorContent === 'function') {
|
110 | return errorContent(toString(input));
|
111 | }
|
112 |
|
113 | return errorContent;
|
114 | };
|
115 | }
|
116 |
|
117 | var validators = {
|
118 | lengthMoreThan: function lengthMoreThan(length, errorContent) {
|
119 | return validate((0, _predicates.lengthMoreThan)(length), errorContent);
|
120 | },
|
121 | lengthLessThan: function lengthLessThan(length, errorContent) {
|
122 | return validate((0, _predicates.lengthLessThan)(length), errorContent);
|
123 | },
|
124 | numericString: function numericString(errorContent) {
|
125 | return validate(_predicates.isNumericString, errorContent);
|
126 | },
|
127 | positiveNumericString: function positiveNumericString(errorContent) {
|
128 | return validate(_predicates.isPositiveNumericString, errorContent);
|
129 | },
|
130 | nonNumericString: function nonNumericString(errorContent) {
|
131 | return validate(_predicates.notNumericString, errorContent);
|
132 | },
|
133 | requiredString: function requiredString(errorContent) {
|
134 | return validateRequired(_predicates.notEmptyString, errorContent);
|
135 | },
|
136 | required: function required(errorContent) {
|
137 | return validateRequired(_predicates.notEmpty, errorContent);
|
138 | }
|
139 | };
|
140 |
|
141 | function toString(obj) {
|
142 | if (obj == null) {
|
143 | return '';
|
144 | }
|
145 |
|
146 | return obj.toString();
|
147 | }
|
148 |
|
149 | var _default = validators;
|
150 | exports["default"] = _default; |
\ | No newline at end of file |