diff --git a/src/ParseSchema.js b/src/ParseSchema.js index 49c717f0d..16174c8d6 100644 --- a/src/ParseSchema.js +++ b/src/ParseSchema.js @@ -241,6 +241,14 @@ class ParseSchema { if (options.defaultValue !== undefined) { fieldOptions.defaultValue = options.defaultValue; } + if (type === 'Date') { + if (options && options.defaultValue) { + fieldOptions.defaultValue = { + __type: 'Date', + iso: new Date(options.defaultValue), + }; + } + } this._fields[name] = fieldOptions; return this; } @@ -310,12 +318,6 @@ class ParseSchema { * @returns {Parse.Schema} Returns the schema, so you can chain this call. */ addDate(name: string, options: FieldOptions) { - if (options && options.defaultValue) { - options.defaultValue = { - __type: 'Date', - iso: new Date(options.defaultValue), - }; - } return this.addField(name, 'Date', options); } diff --git a/src/__tests__/ParseSchema-test.js b/src/__tests__/ParseSchema-test.js index 983c1a247..409ecb507 100644 --- a/src/__tests__/ParseSchema-test.js +++ b/src/__tests__/ParseSchema-test.js @@ -194,6 +194,15 @@ describe('ParseSchema', () => { } }); + it('can add date field with default value', () => { + const schema = new ParseSchema('NewSchemaTest'); + const date = new Date(); + schema.addDate('testField', { defaultValue: date }); + schema.addField('testField2', 'Date', { defaultValue: date }); + expect(schema._fields.testField.defaultValue).toEqual({ __type: 'Date', iso: date }); + expect(schema._fields.testField2.defaultValue).toEqual({ __type: 'Date', iso: date }); + }); + it('cannot add index with null name', done => { try { const schema = new ParseSchema('SchemaTest');