UNPKG

5.61 kBJavaScriptView Raw
1import _extends from "@babel/runtime/helpers/esm/extends";
2import inherits from './util/inherits';
3import isAbsent from './util/isAbsent';
4import isSchema from './util/isSchema';
5import printValue from './util/printValue';
6import MixedSchema from './mixed';
7import { array as locale } from './locale';
8import runTests from './util/runTests';
9export default ArraySchema;
10
11function ArraySchema(type) {
12 var _this = this;
13
14 if (!(this instanceof ArraySchema)) return new ArraySchema(type);
15 MixedSchema.call(this, {
16 type: 'array'
17 }); // `undefined` specifically means uninitialized, as opposed to
18 // "no subtype"
19
20 this._subType = undefined;
21 this.innerType = undefined;
22 this.withMutation(function () {
23 _this.transform(function (values) {
24 if (typeof values === 'string') try {
25 values = JSON.parse(values);
26 } catch (err) {
27 values = null;
28 }
29 return this.isType(values) ? values : null;
30 });
31
32 if (type) _this.of(type);
33 });
34}
35
36inherits(ArraySchema, MixedSchema, {
37 _typeCheck: function _typeCheck(v) {
38 return Array.isArray(v);
39 },
40 _cast: function _cast(_value, _opts) {
41 var _this2 = this;
42
43 var value = MixedSchema.prototype._cast.call(this, _value, _opts); //should ignore nulls here
44
45
46 if (!this._typeCheck(value) || !this.innerType) return value;
47 var isChanged = false;
48 var castArray = value.map(function (v, idx) {
49 var castElement = _this2.innerType.cast(v, _extends({}, _opts, {
50 path: (_opts.path || '') + "[" + idx + "]"
51 }));
52
53 if (castElement !== v) {
54 isChanged = true;
55 }
56
57 return castElement;
58 });
59 return isChanged ? castArray : value;
60 },
61 _validate: function _validate(_value, options, callback) {
62 var _this3 = this;
63
64 if (options === void 0) {
65 options = {};
66 }
67
68 var errors = [];
69 var sync = options.sync;
70 var path = options.path;
71 var innerType = this.innerType;
72
73 var endEarly = this._option('abortEarly', options);
74
75 var recursive = this._option('recursive', options);
76
77 var originalValue = options.originalValue != null ? options.originalValue : _value;
78
79 MixedSchema.prototype._validate.call(this, _value, options, function (err, value) {
80 if (err) {
81 if (endEarly) return void callback(err);
82 errors.push(err);
83 value = err.value;
84 }
85
86 if (!recursive || !innerType || !_this3._typeCheck(value)) {
87 callback(errors[0] || null, value);
88 return;
89 }
90
91 originalValue = originalValue || value; // #950 Ensure that sparse array empty slots are validated
92
93 var tests = new Array(value.length);
94
95 var _loop = function _loop(idx) {
96 var item = value[idx];
97 var path = (options.path || '') + "[" + idx + "]"; // object._validate note for isStrict explanation
98
99 var innerOptions = _extends({}, options, {
100 path: path,
101 strict: true,
102 parent: value,
103 index: idx,
104 originalValue: originalValue[idx]
105 });
106
107 tests[idx] = function (_, cb) {
108 return innerType.validate ? innerType.validate(item, innerOptions, cb) : cb(null);
109 };
110 };
111
112 for (var idx = 0; idx < value.length; idx++) {
113 _loop(idx);
114 }
115
116 runTests({
117 sync: sync,
118 path: path,
119 value: value,
120 errors: errors,
121 endEarly: endEarly,
122 tests: tests
123 }, callback);
124 });
125 },
126 of: function of(schema) {
127 var next = this.clone();
128 if (schema !== false && !isSchema(schema)) throw new TypeError('`array.of()` sub-schema must be a valid yup schema, or `false` to negate a current sub-schema. ' + 'not: ' + printValue(schema));
129 next._subType = schema;
130 next.innerType = schema;
131 return next;
132 },
133 min: function min(_min, message) {
134 message = message || locale.min;
135 return this.test({
136 message: message,
137 name: 'min',
138 exclusive: true,
139 params: {
140 min: _min
141 },
142 test: function test(value) {
143 return isAbsent(value) || value.length >= this.resolve(_min);
144 }
145 });
146 },
147 max: function max(_max, message) {
148 message = message || locale.max;
149 return this.test({
150 message: message,
151 name: 'max',
152 exclusive: true,
153 params: {
154 max: _max
155 },
156 test: function test(value) {
157 return isAbsent(value) || value.length <= this.resolve(_max);
158 }
159 });
160 },
161 length: function length(_length, message) {
162 message = message || locale.length;
163 return this.test({
164 message: message,
165 name: 'length',
166 exclusive: true,
167 params: {
168 length: _length
169 },
170 test: function test(value) {
171 return isAbsent(value) || value.length === this.resolve(_length);
172 }
173 });
174 },
175 ensure: function ensure() {
176 var _this4 = this;
177
178 return this.default(function () {
179 return [];
180 }).transform(function (val, original) {
181 // We don't want to return `null` for nullable schema
182 if (_this4._typeCheck(val)) return val;
183 return original == null ? [] : [].concat(original);
184 });
185 },
186 compact: function compact(rejector) {
187 var reject = !rejector ? function (v) {
188 return !!v;
189 } : function (v, i, a) {
190 return !rejector(v, i, a);
191 };
192 return this.transform(function (values) {
193 return values != null ? values.filter(reject) : values;
194 });
195 },
196 describe: function describe() {
197 var base = MixedSchema.prototype.describe.call(this);
198 if (this.innerType) base.innerType = this.innerType.describe();
199 return base;
200 }
201});
\No newline at end of file