Repository: jquense/yup
Pull request #1119 (2020-11-19 19:51:04)

feat: deprecate the getter overload of `default()`


Patch:
diff --git a/README.md b/README.md
index 5973acd..e637630 100644
--- a/README.md
+++ b/README.md
@@ -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`
 
@@ -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`
@@ -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`
diff --git a/src/mixed.js b/src/mixed.js
index a69d28a..af2735d 100644
--- a/src/mixed.js
+++ b/src/mixed.js
@@ -240,7 +240,7 @@ const proto = (SchemaType.prototype = {
           );
 
     if (value === undefined && has(this, '_default')) {
-      value = this.default();
+      value = this.getDefault();
     }
 
     return value;
@@ -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();
diff --git a/src/object.js b/src/object.js
index 30080cd..5e7dbcb 100644
--- a/src/object.js
+++ b/src/object.js
@@ -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;
@@ -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;
 


Test patch:
diff --git a/test/array.js b/test/array.js
index ff4d60e..90d83ce 100644
--- a/test/array.js
+++ b/test/array.js
@@ -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]);
   });
 
diff --git a/test/bool.js b/test/bool.js
index 10155a8..ca27567 100644
--- a/test/bool.js
+++ b/test/bool.js
@@ -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', () => {
@@ -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');
         }),
diff --git a/test/mixed.js b/test/mixed.js
index 729c405..cdd8174 100644
--- a/test/mixed.js
+++ b/test/mixed.js
@@ -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,
     );
   });
@@ -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 () {
diff --git a/test/number.js b/test/number.js
index 52c509e..6c1eeb6 100644
--- a/test/number.js
+++ b/test/number.js
@@ -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 () {
diff --git a/test/object.js b/test/object.js
index 2feefb8..e1fbb66 100644
--- a/test/object.js
+++ b/test/object.js
@@ -251,7 +251,7 @@ describe('Object types', () => {
     });
 
     it('should expand objects by default', () => {
-      objSchema.default().should.eql({
+      objSchema.getDefault().should.eql({
         nest: { str: 'hi' },
       });
     });
@@ -259,7 +259,7 @@ describe('Object types', () => {
     it('should accept a user provided default', () => {
       objSchema = objSchema.default({ boom: 'hi' });
 
-      objSchema.default().should.eql({
+      objSchema.getDefault().should.eql({
         boom: 'hi',
       });
     });
@@ -269,7 +269,7 @@ describe('Object types', () => {
         str: string(),
         nest: object({ str: string() }),
       })
-        .default()
+        .getDefault()
         .should.eql({
           nest: { str: undefined },
           str: undefined,
@@ -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', () => {
@@ -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',
     });
@@ -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',
     });
 
diff --git a/test/string.js b/test/string.js
index cd79a64..eaed092 100644
--- a/test/string.js
+++ b/test/string.js
@@ -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 () {