UNPKG

3.4 kBJavaScriptView Raw
1import { lengthLessThan as _lengthLessThan, lengthMoreThan as _lengthMoreThan, notEmpty, notEmptyString, isEmpty, notNumericString, isNumericString, isPositiveNumericString } from '@shopify/predicates';
2import { mapObject } from './utilities';
3export function validateNested(validatorDictionary) {
4 return function (input, fields) {
5 var errors = mapObject(input, function (value, field) {
6 var validate = validatorDictionary[field];
7
8 if (validate == null) {
9 return null;
10 }
11
12 if (typeof validate === 'function') {
13 return validate(value, fields);
14 }
15
16 if (!Array.isArray(validate)) {
17 return;
18 }
19
20 var errors = validate.map(function (validator) {
21 return validator(value, fields);
22 }).filter(function (input) {
23 return input != null;
24 });
25
26 if (errors.length === 0) {
27 return;
28 }
29
30 return errors;
31 });
32 var anyErrors = Object.keys(errors).map(function (key) {
33 return errors[key];
34 }).some(function (value) {
35 return value != null;
36 });
37
38 if (anyErrors) {
39 return errors;
40 }
41 };
42}
43export function validateList(validatorDictionary) {
44 var validateItem = validateNested(validatorDictionary);
45 return function (input, fields) {
46 var errors = input.map(function (item) {
47 return validateItem(item, fields);
48 });
49
50 if (errors.some(function (error) {
51 return error != null;
52 })) {
53 return errors;
54 }
55 };
56}
57export function validateWithFields(matcher, errorContent) {
58 return validate(matcher, errorContent);
59}
60export function validate(matcher, errorContent) {
61 return function (input, fields) {
62 var matches = matcher(input, fields);
63 /*
64 always mark empty fields valid to match Polaris guidelines
65 https://polaris.shopify.com/patterns/error-messages#section-form-validation
66 */
67
68 if (isEmpty(input)) {
69 return;
70 }
71
72 if (matches) {
73 return;
74 }
75
76 if (typeof errorContent === 'function') {
77 return errorContent(toString(input));
78 }
79
80 return errorContent;
81 };
82}
83export function validateRequired(matcher, errorContent) {
84 return function (input, fields) {
85 var matches = matcher(input, fields);
86
87 if (matches) {
88 return;
89 }
90
91 if (typeof errorContent === 'function') {
92 return errorContent(toString(input));
93 }
94
95 return errorContent;
96 };
97}
98var validators = {
99 lengthMoreThan: function lengthMoreThan(length, errorContent) {
100 return validate(_lengthMoreThan(length), errorContent);
101 },
102 lengthLessThan: function lengthLessThan(length, errorContent) {
103 return validate(_lengthLessThan(length), errorContent);
104 },
105 numericString: function numericString(errorContent) {
106 return validate(isNumericString, errorContent);
107 },
108 positiveNumericString: function positiveNumericString(errorContent) {
109 return validate(isPositiveNumericString, errorContent);
110 },
111 nonNumericString: function nonNumericString(errorContent) {
112 return validate(notNumericString, errorContent);
113 },
114 requiredString: function requiredString(errorContent) {
115 return validateRequired(notEmptyString, errorContent);
116 },
117 required: function required(errorContent) {
118 return validateRequired(notEmpty, errorContent);
119 }
120};
121
122function toString(obj) {
123 if (obj == null) {
124 return '';
125 }
126
127 return obj.toString();
128}
129
130export default validators;
\No newline at end of file