Comparing version 0.30.0 to 0.31.0
@@ -0,1 +1,36 @@ | ||
# [0.31.0](https://github.com/jquense/yup/compare/v0.30.0...v0.31.0) (2020-11-23) | ||
### Bug Fixes | ||
* path params incorrectly mutated ([ba23eb7](https://github.com/jquense/yup/commit/ba23eb7)), closes [#1122](https://github.com/jquense/yup/issues/1122) | ||
### Features | ||
* add array.length() and treat empty arrays as valid for required() ([fbc158d](https://github.com/jquense/yup/commit/fbc158d)) | ||
* add object.pick and object.omit ([425705a](https://github.com/jquense/yup/commit/425705a)) | ||
* deprecate the getter overload of `default()` ([#1119](https://github.com/jquense/yup/issues/1119)) ([5dae837](https://github.com/jquense/yup/commit/5dae837)) | ||
* more strictly coerce strings, exclude arrays and plain objects ([963d2e8](https://github.com/jquense/yup/commit/963d2e8)) | ||
### BREAKING CHANGES | ||
* array().required() will no longer consider an empty array missing and required checks will pass. | ||
To maintain the old behavior change to: | ||
```js | ||
array().required().min(1) | ||
``` | ||
* plain objects and arrays are no long cast to strings automatically | ||
to recreate the old behavior: | ||
```js | ||
string().transform((_, input) => input != null && input.toString ? input.toString() : value); | ||
``` | ||
# [0.30.0](https://github.com/jquense/yup/compare/v0.29.3...v0.30.0) (2020-11-19) | ||
@@ -2,0 +37,0 @@ |
@@ -126,5 +126,2 @@ import _extends from "@babel/runtime/helpers/esm/extends"; | ||
}, | ||
_isPresent: function _isPresent(value) { | ||
return MixedSchema.prototype._isPresent.call(this, value) && value.length > 0; | ||
}, | ||
of: function of(schema) { | ||
@@ -165,2 +162,16 @@ var next = this.clone(); | ||
}, | ||
length: function length(_length, message) { | ||
message = message || locale.length; | ||
return this.test({ | ||
message: message, | ||
name: 'length', | ||
exclusive: true, | ||
params: { | ||
length: _length | ||
}, | ||
test: function test(value) { | ||
return isAbsent(value) || value.length === this.resolve(_length); | ||
} | ||
}); | ||
}, | ||
ensure: function ensure() { | ||
@@ -167,0 +178,0 @@ var _this4 = this; |
@@ -243,3 +243,3 @@ import _extends from "@babel/runtime/helpers/esm/extends"; | ||
if (value === undefined && has(this, '_default')) { | ||
value = this.default(); | ||
value = this.getDefault(); | ||
} | ||
@@ -362,2 +362,6 @@ | ||
}, | ||
_getDefault: function _getDefault() { | ||
var defaultValue = has(this, '_default') ? this._default : this._defaultDefault; | ||
return typeof defaultValue === 'function' ? defaultValue.call(this) : cloneDeepWith(defaultValue); | ||
}, | ||
getDefault: function getDefault(options) { | ||
@@ -369,8 +373,8 @@ if (options === void 0) { | ||
var schema = this.resolve(options); | ||
return schema.default(); | ||
return schema._getDefault(); | ||
}, | ||
default: function _default(def) { | ||
if (arguments.length === 0) { | ||
var defaultValue = has(this, '_default') ? this._default : this._defaultDefault; | ||
return typeof defaultValue === 'function' ? defaultValue.call(this) : cloneDeepWith(defaultValue); | ||
console.warn('Calling `schema.default()` as a getter to retrieve a default is deprecated and will be removed in the next version. \n' + 'Use `schema.getDefault()` instead.'); | ||
return this._getDefault(); | ||
} | ||
@@ -377,0 +381,0 @@ |
@@ -46,3 +46,3 @@ import _extends from "@babel/runtime/helpers/esm/extends"; | ||
this._nodes.forEach(function (key) { | ||
dft[key] = _this.fields[key].default ? _this.fields[key].default() : undefined; | ||
dft[key] = _this.fields[key].default ? _this.fields[key].getDefault() : undefined; | ||
}); | ||
@@ -90,3 +90,3 @@ | ||
if (value === undefined) return this.default(); | ||
if (value === undefined) return this.getDefault(); | ||
if (!this._typeCheck(value)) return value; | ||
@@ -274,2 +274,29 @@ var fields = this.fields; | ||
}, | ||
pick: function pick(keys) { | ||
var picked = {}; | ||
for (var _iterator2 = _createForOfIteratorHelperLoose(keys), _step2; !(_step2 = _iterator2()).done;) { | ||
var key = _step2.value; | ||
if (this.fields[key]) picked[key] = this.fields[key]; | ||
} | ||
return this.clone().withMutation(function (next) { | ||
next.fields = {}; | ||
return next.shape(picked); | ||
}); | ||
}, | ||
omit: function omit(keys) { | ||
var next = this.clone(); | ||
var fields = next.fields; | ||
next.fields = {}; | ||
for (var _iterator3 = _createForOfIteratorHelperLoose(keys), _step3; !(_step3 = _iterator3()).done;) { | ||
var key = _step3.value; | ||
delete fields[key]; | ||
} | ||
return next.withMutation(function (next) { | ||
return next.shape(fields); | ||
}); | ||
}, | ||
from: function from(_from, to, alias) { | ||
@@ -276,0 +303,0 @@ var fromGetter = getter(_from, true); |
@@ -16,2 +16,3 @@ import inherits from './util/inherits'; | ||
var objStringTag = {}.toString(); | ||
export default function StringSchema() { | ||
@@ -27,3 +28,6 @@ var _this = this; | ||
if (this.isType(value)) return value; | ||
return value != null && value.toString ? value.toString() : value; | ||
if (Array.isArray(value)) return value; | ||
var strValue = value != null && value.toString ? value.toString() : value; | ||
if (strValue === objStringTag) return value; | ||
return strValue; | ||
}); | ||
@@ -38,3 +42,3 @@ }); | ||
_isPresent: function _isPresent(value) { | ||
return MixedSchema.prototype._isPresent.call(this, value) && value.length > 0; | ||
return MixedSchema.prototype._isPresent.call(this, value) && !!value.length; | ||
}, | ||
@@ -41,0 +45,0 @@ length: function length(_length, message) { |
@@ -0,1 +1,2 @@ | ||
import _extends from "@babel/runtime/helpers/esm/extends"; | ||
import printValue from './util/printValue'; | ||
@@ -27,3 +28,6 @@ var strReg = /\$\{\s*(\w+)\s*\}/g; | ||
ValidationError.formatError = function (message, params) { | ||
params.path = params.label || params.path || 'this'; | ||
var path = params.label || params.path || 'this'; | ||
if (path !== params.path) params = _extends({}, params, { | ||
path: path | ||
}); | ||
if (typeof message === 'string') return message.replace(strReg, function (_, key) { | ||
@@ -30,0 +34,0 @@ return printValue(params[key]); |
@@ -144,5 +144,2 @@ "use strict"; | ||
}, | ||
_isPresent: function _isPresent(value) { | ||
return _mixed.default.prototype._isPresent.call(this, value) && value.length > 0; | ||
}, | ||
of: function of(schema) { | ||
@@ -183,2 +180,16 @@ var next = this.clone(); | ||
}, | ||
length: function length(_length, message) { | ||
message = message || _locale.array.length; | ||
return this.test({ | ||
message: message, | ||
name: 'length', | ||
exclusive: true, | ||
params: { | ||
length: _length | ||
}, | ||
test: function test(value) { | ||
return (0, _isAbsent.default)(value) || value.length === this.resolve(_length); | ||
} | ||
}); | ||
}, | ||
ensure: function ensure() { | ||
@@ -185,0 +196,0 @@ var _this4 = this; |
@@ -262,3 +262,3 @@ "use strict"; | ||
if (value === undefined && (0, _has.default)(this, '_default')) { | ||
value = this.default(); | ||
value = this.getDefault(); | ||
} | ||
@@ -381,2 +381,6 @@ | ||
}, | ||
_getDefault: function _getDefault() { | ||
var defaultValue = (0, _has.default)(this, '_default') ? this._default : this._defaultDefault; | ||
return typeof defaultValue === 'function' ? defaultValue.call(this) : (0, _cloneDeepWith.default)(defaultValue); | ||
}, | ||
getDefault: function getDefault(options) { | ||
@@ -388,8 +392,8 @@ if (options === void 0) { | ||
var schema = this.resolve(options); | ||
return schema.default(); | ||
return schema._getDefault(); | ||
}, | ||
default: function _default(def) { | ||
if (arguments.length === 0) { | ||
var defaultValue = (0, _has.default)(this, '_default') ? this._default : this._defaultDefault; | ||
return typeof defaultValue === 'function' ? defaultValue.call(this) : (0, _cloneDeepWith.default)(defaultValue); | ||
console.warn('Calling `schema.default()` as a getter to retrieve a default is deprecated and will be removed in the next version. \n' + 'Use `schema.getDefault()` instead.'); | ||
return this._getDefault(); | ||
} | ||
@@ -396,0 +400,0 @@ |
@@ -65,3 +65,3 @@ "use strict"; | ||
this._nodes.forEach(function (key) { | ||
dft[key] = _this.fields[key].default ? _this.fields[key].default() : undefined; | ||
dft[key] = _this.fields[key].default ? _this.fields[key].getDefault() : undefined; | ||
}); | ||
@@ -111,3 +111,3 @@ | ||
if (value === undefined) return this.default(); | ||
if (value === undefined) return this.getDefault(); | ||
if (!this._typeCheck(value)) return value; | ||
@@ -293,2 +293,29 @@ var fields = this.fields; | ||
}, | ||
pick: function pick(keys) { | ||
var picked = {}; | ||
for (var _iterator2 = _createForOfIteratorHelperLoose(keys), _step2; !(_step2 = _iterator2()).done;) { | ||
var key = _step2.value; | ||
if (this.fields[key]) picked[key] = this.fields[key]; | ||
} | ||
return this.clone().withMutation(function (next) { | ||
next.fields = {}; | ||
return next.shape(picked); | ||
}); | ||
}, | ||
omit: function omit(keys) { | ||
var next = this.clone(); | ||
var fields = next.fields; | ||
next.fields = {}; | ||
for (var _iterator3 = _createForOfIteratorHelperLoose(keys), _step3; !(_step3 = _iterator3()).done;) { | ||
var key = _step3.value; | ||
delete fields[key]; | ||
} | ||
return next.withMutation(function (next) { | ||
return next.shape(fields); | ||
}); | ||
}, | ||
from: function from(_from, to, alias) { | ||
@@ -295,0 +322,0 @@ var fromGetter = (0, _propertyExpr.getter)(_from, true); |
@@ -27,2 +27,4 @@ "use strict"; | ||
var objStringTag = {}.toString(); | ||
function StringSchema() { | ||
@@ -40,3 +42,6 @@ var _this = this; | ||
if (this.isType(value)) return value; | ||
return value != null && value.toString ? value.toString() : value; | ||
if (Array.isArray(value)) return value; | ||
var strValue = value != null && value.toString ? value.toString() : value; | ||
if (strValue === objStringTag) return value; | ||
return strValue; | ||
}); | ||
@@ -52,3 +57,3 @@ }); | ||
_isPresent: function _isPresent(value) { | ||
return _mixed.default.prototype._isPresent.call(this, value) && value.length > 0; | ||
return _mixed.default.prototype._isPresent.call(this, value) && !!value.length; | ||
}, | ||
@@ -55,0 +60,0 @@ length: function length(_length, message) { |
@@ -8,2 +8,4 @@ "use strict"; | ||
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); | ||
var _printValue = _interopRequireDefault(require("./util/printValue")); | ||
@@ -38,3 +40,6 @@ | ||
ValidationError.formatError = function (message, params) { | ||
params.path = params.label || params.path || 'this'; | ||
var path = params.label || params.path || 'this'; | ||
if (path !== params.path) params = (0, _extends2.default)({}, params, { | ||
path: path | ||
}); | ||
if (typeof message === 'string') return message.replace(strReg, function (_, key) { | ||
@@ -41,0 +46,0 @@ return (0, _printValue.default)(params[key]); |
{ | ||
"name": "yup", | ||
"version": "0.30.0", | ||
"version": "0.31.0", | ||
"description": "Dead simple Object schema validation", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -41,6 +41,6 @@ # Yup | ||
- [`mixed.default(value: any): Schema`](#mixeddefaultvalue-any-schema) | ||
- [`mixed.default(): Any`](#mixeddefault-any) | ||
- [`mixed.getDefault(options?: object): Any`](#mixedgetdefaultoptions-object-any) | ||
- [`mixed.nullable(isNullable: boolean = true): Schema`](#mixednullableisnullable-boolean--true-schema) | ||
- [`mixed.required(message?: string | function): Schema`](#mixedrequiredmessage-string--function-schema) | ||
- [`mixed.notRequired(): Schema`](#mixednotrequired-schema) | ||
- [`mixed.notRequired(): Schema` Alias: `optional()`](#mixednotrequired-schema-alias-optional) | ||
- [`mixed.defined(): Schema`](#mixeddefined-schema) | ||
@@ -84,3 +84,3 @@ - [`mixed.typeError(message: string): Schema`](#mixedtypeerrormessage-string-schema) | ||
- [`array.of(type: Schema): Schema`](#arrayoftype-schema-schema) | ||
- [`array.required(message?: string | function): Schema`](#arrayrequiredmessage-string--function-schema) | ||
- [`array.length(length: number | Ref, message?: string | function): Schema`](#arraylengthlength-number--ref-message-string--function-schema) | ||
- [`array.min(limit: number | Ref, message?: string | function): Schema`](#arrayminlimit-number--ref-message-string--function-schema) | ||
@@ -93,2 +93,4 @@ - [`array.max(limit: number | Ref, message?: string | function): Schema`](#arraymaxlimit-number--ref-message-string--function-schema) | ||
- [`object.shape(fields: object, noSortEdges?: Array<[string, string]>): Schema`](#objectshapefields-object-nosortedges-arraystring-string-schema) | ||
- [`object.pick(keys: string[]): Schema`](#objectpickkeys-string-schema) | ||
- [`object.omit(keys: string[]): Schema`](#objectomitkeys-string-schema) | ||
- [`object.from(fromKey: string, toKey: string, alias: boolean = false): Schema`](#objectfromfromkey-string-tokey-string-alias-boolean--false-schema) | ||
@@ -575,5 +577,5 @@ - [`object.noUnknown(onlyKnownKeys: boolean = true, message?: string | function): Schema`](#objectnounknownonlyknownkeys-boolean--true-message-string--function-schema) | ||
#### `mixed.default(): Any` | ||
#### `mixed.getDefault(options?: object): Any` | ||
Calling `default` with no arguments will return the current default value | ||
Retrieve a previously set default value. `getDefault` will resolve any conditions that may alter the default. Optionally pass `options` with `context` (for more info on `context` see `mixed.validate`). | ||
@@ -587,11 +589,15 @@ #### `mixed.nullable(isNullable: boolean = true): Schema` | ||
Mark the schema as required. All field values apart from `undefined` and `null` meet this requirement. | ||
Mark the schema as required, which will not allow `undefined` or `null` as a value. | ||
Note that unless a schema is marked as `nullable()` a `null` value is treated as a type error, not a missing value. Mark a schema as `mixed().nullable().required()` treat `null` as missing. | ||
#### `mixed.notRequired(): Schema` | ||
> Watch out! [`string().required`](#stringrequiredmessage-string--function-schema)) works a little | ||
> different and additionally prevents empty string values (`''`) when required. | ||
Mark the schema as not required. Passing `undefined` as value will not fail validation. | ||
#### `mixed.notRequired(): Schema` Alias: `optional()` | ||
Mark the schema as not required. Passing `undefined` (or `null` for nullable schema) as value will not fail validation. | ||
#### `mixed.defined(): Schema` | ||
Mark the schema as required but nullable. All field values apart from `undefined` meet this requirement. | ||
Require a value for the schema. All field values apart from `undefined` meet this requirement. | ||
@@ -731,3 +737,3 @@ #### `mixed.typeError(message: string): Schema` | ||
but now it's exposed too as a second argument of the test functions. It's allow you decide which | ||
approach you prefer. | ||
approach you prefer. | ||
@@ -833,3 +839,3 @@ - `this.path`: the string path of the current validation | ||
The same as the `mixed()` schema required, except that empty strings are also considered 'missing' values. | ||
The same as the `mixed()` schema required, **except** that empty strings are also considered 'missing' values. | ||
@@ -1026,5 +1032,5 @@ #### `string.length(limit: number | Ref, message?: string | function): Schema` | ||
#### `array.required(message?: string | function): Schema` | ||
#### `array.length(length: number | Ref, message?: string | function): Schema` | ||
The same as the `mixed()` schema required, except that empty arrays are also considered 'missing' values. | ||
Set a specific length requirement for the array. The `${length}` interpolation can be used in the `message` argument. | ||
@@ -1160,2 +1166,32 @@ #### `array.min(limit: number | Ref, message?: string | function): Schema` | ||
#### `object.pick(keys: string[]): Schema` | ||
Create a new schema from a subset of the original's fields. | ||
```js | ||
const person = object({ | ||
age: number().default(30).required(), | ||
name: string().default('pat').required(), | ||
color: string().default('red').required(), | ||
}); | ||
const nameAndAge = person.pick(['name', 'age']); | ||
nameAndAge.getDefault(); // => { age: 30, name: 'pat'} | ||
``` | ||
#### `object.omit(keys: string[]): Schema` | ||
Create a new schema with fields omitted. | ||
```js | ||
const person = object({ | ||
age: number().default(30).required(), | ||
name: string().default('pat').required(), | ||
color: string().default('red').required(), | ||
}); | ||
const nameAndAge = person.omit('color']); | ||
nameAndAge.getDefault(); // => { age: 30, name: 'pat'} | ||
``` | ||
#### `object.from(fromKey: string, toKey: string, alias: boolean = false): Schema` | ||
@@ -1300,4 +1336,3 @@ | ||
// TypeScript. Both will have the same effect on the resulting type by | ||
// excluding `undefined`, but `required` will also disallow other values | ||
// such as empty strings. | ||
// excluding `undefined`, but `required` will also disallow empty strings. | ||
.defined(), | ||
@@ -1304,0 +1339,0 @@ nickName: yup |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
234786
4432
1410