UNPKG

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