Skip to content
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
14 changes: 8 additions & 6 deletions src/ParseSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}

Expand Down
9 changes: 9 additions & 0 deletions src/__tests__/ParseSchema-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down