Skip to content

Parse.Schema required fields and defaultValues #961

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

Merged
merged 7 commits into from
Oct 16, 2019
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
91 changes: 91 additions & 0 deletions integration/test/ParseSchemaTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const defaultCLPS = {
protectedFields: { '*': [] },
};

const TestObject = Parse.Object.extend('TestObject');

describe('Schema', () => {
beforeAll(() => {
Parse.initialize('integration');
Expand Down Expand Up @@ -104,6 +106,95 @@ describe('Schema', () => {
});
});

it('save required and default values', async () => {
const testSchema = new Parse.Schema('SchemaTest');
testSchema.addField('fieldString', 'String', { required: true, defaultValue: 'Hello World' });
const schema = await testSchema.save();
assert.deepEqual(schema.fields.fieldString, {
type: 'String', required: true, defaultValue: 'Hello World'
})
const object = new Parse.Object('SchemaTest');
await object.save();
assert.equal(object.get('fieldString'), 'Hello World');
});

it('save required and default pointer values', async () => {
const pointer = new TestObject();
await pointer.save();
const testSchema = new Parse.Schema('SchemaTest');
testSchema
.addPointer('pointerField', 'TestObject', { required: true, defaultValue: pointer })
.addPointer('pointerJSONField', 'TestObject', { required: true, defaultValue: pointer.toPointer() })
const schema = await testSchema.save();
assert.deepEqual(schema.fields.pointerField, schema.fields.pointerJSONField);
assert.deepEqual(schema.fields.pointerField.defaultValue, pointer.toPointer());
assert.equal(schema.fields.pointerField.required, true);
});

it('set multiple required and default values', async () => {
const point = new Parse.GeoPoint(44.0, -11.0);
const polygon = new Parse.Polygon([[0,0], [0,1], [1,1], [1,0]]);
const file = new Parse.File('parse-server-logo', { base64: 'ParseA==' });
await file.save();
const testSchema = new Parse.Schema('SchemaFieldTest');

testSchema
.addField('defaultFieldString', 'String', { required: true, defaultValue: 'hello' })
.addString('stringField', { required: true, defaultValue: 'world' })
.addNumber('numberField', { required: true, defaultValue: 10 })
.addBoolean('booleanField', { required: true, defaultValue: false })
.addDate('dateField', { required: true, defaultValue: new Date('2000-01-01T00:00:00.000Z') })
.addDate('dateStringField', { required: true, defaultValue: '2000-01-01T00:00:00.000Z' })
.addFile('fileField', { required: true, defaultValue: file })
.addGeoPoint('geoPointField', { required: true, defaultValue: point })
.addPolygon('polygonField', { required: true, defaultValue: polygon })
.addArray('arrayField', { required: true, defaultValue: [1, 2, 3] })
.addObject('objectField', { required: true, defaultValue: { foo: 'bar' } })

const schema = await testSchema.save();
assert.deepEqual(schema.fields, {
objectId: { type: 'String' },
updatedAt: { type: 'Date' },
createdAt: { type: 'Date' },
defaultFieldString: { type: 'String', required: true, defaultValue: 'hello' },
stringField: { type: 'String', required: true, defaultValue: 'world' },
numberField: { type: 'Number', required: true, defaultValue: 10 },
booleanField: { type: 'Boolean', required: true, defaultValue: false },
dateField: { type: 'Date', required: true, defaultValue: { __type: 'Date', iso: '2000-01-01T00:00:00.000Z' } },
dateStringField: { type: 'Date', required: true, defaultValue: { __type: 'Date', iso: '2000-01-01T00:00:00.000Z' } },
fileField: { type: 'File', required: true, defaultValue: file.toJSON() },
geoPointField: { type: 'GeoPoint', required: true, defaultValue: point.toJSON() },
polygonField: { type: 'Polygon', required: true, defaultValue: polygon.toJSON() },
arrayField: { type: 'Array', required: true, defaultValue: [1, 2, 3] },
objectField: { type: 'Object', required: true, defaultValue: { foo: 'bar' } },
ACL: { type: 'ACL' }
});
const object = new Parse.Object('SchemaFieldTest');
await object.save();
const json = object.toJSON();
delete json.createdAt;
delete json.updatedAt;
delete json.objectId;

const expected = {
defaultFieldString: 'hello',
stringField: 'world',
numberField: 10,
booleanField: false,
dateField: { __type: 'Date', iso: '2000-01-01T00:00:00.000Z' },
dateStringField: { __type: 'Date', iso: '2000-01-01T00:00:00.000Z' },
fileField: file.toJSON(),
geoPointField: point.toJSON(),
polygonField: {
__type: 'Polygon',
coordinates: [ [ 0, 0 ], [ 0, 1 ], [ 1, 1 ], [ 1, 0 ], [ 0, 0 ] ]
},
arrayField: [ 1, 2, 3 ],
objectField: { foo: 'bar' },
};
assert.deepEqual(json, expected);
});

it('save class level permissions', async () => {
const clp = {
get: { requiresAuthentication: true },
Expand Down
Loading