Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: deprecate the getter overload of default() #1119

Merged
merged 1 commit into from
Nov 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -568,9 +568,9 @@ yup.object.default(() => ({ number: 5 })); // this is cheaper
yup.date.default(() => new Date()); // also helpful for defaults that change over time
```

#### `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`).

#### `mixed.nullable(isNullable: boolean = true): Schema`

Expand Down Expand Up @@ -1164,7 +1164,7 @@ const person = object({
});

const nameAndAge = person.pick(['name', 'age']);
nameAndAge.default(); // => { age: 30, name: 'pat'}
nameAndAge.getDefault(); // => { age: 30, name: 'pat'}
```

#### `object.omit(keys: string[]): Schema`
Expand All @@ -1179,7 +1179,7 @@ const person = object({
});

const nameAndAge = person.omit('color']);
nameAndAge.default(); // => { age: 30, name: 'pat'}
nameAndAge.getDefault(); // => { age: 30, name: 'pat'}
```

#### `object.from(fromKey: string, toKey: string, alias: boolean = false): Schema`
Expand Down
25 changes: 17 additions & 8 deletions src/mixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ const proto = (SchemaType.prototype = {
);

if (value === undefined && has(this, '_default')) {
value = this.default();
value = this.getDefault();
}

return value;
Expand Down Expand Up @@ -352,20 +352,29 @@ const proto = (SchemaType.prototype = {
}
},

_getDefault() {
var defaultValue = has(this, '_default')
? this._default
: this._defaultDefault;

return typeof defaultValue === 'function'
? defaultValue.call(this)
: cloneDeepWith(defaultValue);
},

getDefault(options = {}) {
let schema = this.resolve(options);
return schema.default();
return schema._getDefault();
},

default(def) {
if (arguments.length === 0) {
var defaultValue = has(this, '_default')
? this._default
: this._defaultDefault;
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 typeof defaultValue === 'function'
? defaultValue.call(this)
: cloneDeepWith(defaultValue);
return this._getDefault();
}

var next = this.clone();
Expand Down
4 changes: 2 additions & 2 deletions src/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default function ObjectSchema(spec) {
let dft = {};
this._nodes.forEach((key) => {
dft[key] = this.fields[key].default
? this.fields[key].default()
? this.fields[key].getDefault()
: undefined;
});
return dft;
Expand Down Expand Up @@ -73,7 +73,7 @@ inherits(ObjectSchema, MixedSchema, {
let value = MixedSchema.prototype._cast.call(this, _value);

//should ignore nulls here
if (value === undefined) return this.default();
if (value === undefined) return this.getDefault();

if (!this._typeCheck(value)) return value;

Expand Down
4 changes: 2 additions & 2 deletions test/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ describe('Array types', () => {
});

it('should handle DEFAULT', () => {
expect(array().default()).to.equal(undefined);
expect(array().getDefault()).to.equal(undefined);

array()
.default(() => [1, 2, 3])
.default()
.getDefault()
.should.eql([1, 2, 3]);
});

Expand Down
32 changes: 7 additions & 25 deletions test/bool.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,8 @@ describe('Boolean types', () => {
it('should handle DEFAULT', () => {
let inst = bool();

expect(inst.default()).to.equal(undefined);
inst
.default(true)
.required()
.default()
.should.equal(true);
expect(inst.getDefault()).to.equal(undefined);
inst.default(true).required().getDefault().should.equal(true);
});

it('should type check', () => {
Expand All @@ -41,34 +37,20 @@ describe('Boolean types', () => {

expect(inst.isType(null)).to.equal(false);

inst
.nullable()
.isType(null)
.should.equal(true);
inst.nullable().isType(null).should.equal(true);
});

it('bool should VALIDATE correctly', () => {
let inst = bool().required();

return Promise.all([
bool()
.isValid('1')
.should.eventually()
.equal(true),
bool()
.strict()
.isValid(null)
.should.eventually()
.equal(false),
bool()
.nullable()
.isValid(null)
.should.eventually()
.equal(true),
bool().isValid('1').should.eventually().equal(true),
bool().strict().isValid(null).should.eventually().equal(false),
bool().nullable().isValid(null).should.eventually().equal(true),
inst
.validate()
.should.be.rejected()
.then(err => {
.then((err) => {
err.errors.length.should.equal(1);
err.errors[0].should.contain('required');
}),
Expand Down
4 changes: 2 additions & 2 deletions test/mixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ describe('Mixed Types ', () => {
it('concat should maintain undefined defaults', function () {
let inst = string().default('hi');

expect(inst.concat(string().default(undefined)).default()).to.equal(
expect(inst.concat(string().default(undefined)).getDefault()).to.equal(
undefined,
);
});
Expand Down Expand Up @@ -810,7 +810,7 @@ describe('Mixed Types ', () => {
}),
});

inst.default().should.eql({ prop: undefined });
inst.getDefault().should.eql({ prop: undefined });
});

it('should support self references in conditions', async function () {
Expand Down
4 changes: 2 additions & 2 deletions test/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ describe('Number types', function () {
it('should handle DEFAULT', function () {
var inst = number().default(0);

inst.default().should.equal(0);
inst.default(5).required().default().should.equal(5);
inst.getDefault().should.equal(0);
inst.default(5).required().getDefault().should.equal(5);
});

it('should type check', function () {
Expand Down
14 changes: 7 additions & 7 deletions test/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,15 +251,15 @@ describe('Object types', () => {
});

it('should expand objects by default', () => {
objSchema.default().should.eql({
objSchema.getDefault().should.eql({
nest: { str: 'hi' },
});
});

it('should accept a user provided default', () => {
objSchema = objSchema.default({ boom: 'hi' });

objSchema.default().should.eql({
objSchema.getDefault().should.eql({
boom: 'hi',
});
});
Expand All @@ -269,7 +269,7 @@ describe('Object types', () => {
str: string(),
nest: object({ str: string() }),
})
.default()
.getDefault()
.should.eql({
nest: { str: undefined },
str: undefined,
Expand Down Expand Up @@ -793,9 +793,9 @@ describe('Object types', () => {
other: bool(),
}).default(undefined);

expect(inst.concat(object()).default()).to.equal(undefined);
expect(inst.concat(object()).getDefault()).to.equal(undefined);

expect(inst.concat(object().default({})).default()).to.eql({});
expect(inst.concat(object().default({})).getDefault()).to.eql({});
});

it('should handle nested conditionals', () => {
Expand Down Expand Up @@ -886,7 +886,7 @@ describe('Object types', () => {
color: string().default('red').required(),
});

expect(inst.pick(['age', 'name']).default()).to.eql({
expect(inst.pick(['age', 'name']).getDefault()).to.eql({
age: 30,
name: 'pat',
});
Expand All @@ -906,7 +906,7 @@ describe('Object types', () => {
color: string().default('red').required(),
});

expect(inst.omit(['age', 'name']).default()).to.eql({
expect(inst.omit(['age', 'name']).getDefault()).to.eql({
color: 'red',
});

Expand Down
2 changes: 1 addition & 1 deletion test/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe('String types', () => {
it('should handle DEFAULT', function () {
var inst = string();

inst.default('my_value').required().default().should.equal('my_value');
inst.default('my_value').required().getDefault().should.equal('my_value');
});

it('should type check', function () {
Expand Down