Skip to content

Commit 7ff9f92

Browse files
authored
Parse.Schema required fields and defaultValues (#961)
* Parse.Schema required fields and defaultValues Closes: #930 Requires Parse Server 3.7.0+ Feature: parse-community/parse-server#5835 I added a FieldOption to addField and its counter parts. This will allow for future options like uppercase / lowercase for example. I keep getting a schema mismatch when I use dates and pointers. @davimacedo Maybe you know why? * improve coverage * fix date and pointer fields * print tests * fix utc date test * Documentation * nit test
1 parent 41db1dc commit 7ff9f92

File tree

5 files changed

+525
-548
lines changed

5 files changed

+525
-548
lines changed

integration/test/ParseSchemaTest.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ const defaultCLPS = {
2424
protectedFields: { '*': [] },
2525
};
2626

27+
const TestObject = Parse.Object.extend('TestObject');
28+
2729
describe('Schema', () => {
2830
beforeAll(() => {
2931
Parse.initialize('integration');
@@ -104,6 +106,95 @@ describe('Schema', () => {
104106
});
105107
});
106108

109+
it('save required and default values', async () => {
110+
const testSchema = new Parse.Schema('SchemaTest');
111+
testSchema.addField('fieldString', 'String', { required: true, defaultValue: 'Hello World' });
112+
const schema = await testSchema.save();
113+
assert.deepEqual(schema.fields.fieldString, {
114+
type: 'String', required: true, defaultValue: 'Hello World'
115+
})
116+
const object = new Parse.Object('SchemaTest');
117+
await object.save();
118+
assert.equal(object.get('fieldString'), 'Hello World');
119+
});
120+
121+
it('save required and default pointer values', async () => {
122+
const pointer = new TestObject();
123+
await pointer.save();
124+
const testSchema = new Parse.Schema('SchemaTest');
125+
testSchema
126+
.addPointer('pointerField', 'TestObject', { required: true, defaultValue: pointer })
127+
.addPointer('pointerJSONField', 'TestObject', { required: true, defaultValue: pointer.toPointer() })
128+
const schema = await testSchema.save();
129+
assert.deepEqual(schema.fields.pointerField, schema.fields.pointerJSONField);
130+
assert.deepEqual(schema.fields.pointerField.defaultValue, pointer.toPointer());
131+
assert.equal(schema.fields.pointerField.required, true);
132+
});
133+
134+
it('set multiple required and default values', async () => {
135+
const point = new Parse.GeoPoint(44.0, -11.0);
136+
const polygon = new Parse.Polygon([[0,0], [0,1], [1,1], [1,0]]);
137+
const file = new Parse.File('parse-server-logo', { base64: 'ParseA==' });
138+
await file.save();
139+
const testSchema = new Parse.Schema('SchemaFieldTest');
140+
141+
testSchema
142+
.addField('defaultFieldString', 'String', { required: true, defaultValue: 'hello' })
143+
.addString('stringField', { required: true, defaultValue: 'world' })
144+
.addNumber('numberField', { required: true, defaultValue: 10 })
145+
.addBoolean('booleanField', { required: true, defaultValue: false })
146+
.addDate('dateField', { required: true, defaultValue: new Date('2000-01-01T00:00:00.000Z') })
147+
.addDate('dateStringField', { required: true, defaultValue: '2000-01-01T00:00:00.000Z' })
148+
.addFile('fileField', { required: true, defaultValue: file })
149+
.addGeoPoint('geoPointField', { required: true, defaultValue: point })
150+
.addPolygon('polygonField', { required: true, defaultValue: polygon })
151+
.addArray('arrayField', { required: true, defaultValue: [1, 2, 3] })
152+
.addObject('objectField', { required: true, defaultValue: { foo: 'bar' } })
153+
154+
const schema = await testSchema.save();
155+
assert.deepEqual(schema.fields, {
156+
objectId: { type: 'String' },
157+
updatedAt: { type: 'Date' },
158+
createdAt: { type: 'Date' },
159+
defaultFieldString: { type: 'String', required: true, defaultValue: 'hello' },
160+
stringField: { type: 'String', required: true, defaultValue: 'world' },
161+
numberField: { type: 'Number', required: true, defaultValue: 10 },
162+
booleanField: { type: 'Boolean', required: true, defaultValue: false },
163+
dateField: { type: 'Date', required: true, defaultValue: { __type: 'Date', iso: '2000-01-01T00:00:00.000Z' } },
164+
dateStringField: { type: 'Date', required: true, defaultValue: { __type: 'Date', iso: '2000-01-01T00:00:00.000Z' } },
165+
fileField: { type: 'File', required: true, defaultValue: file.toJSON() },
166+
geoPointField: { type: 'GeoPoint', required: true, defaultValue: point.toJSON() },
167+
polygonField: { type: 'Polygon', required: true, defaultValue: polygon.toJSON() },
168+
arrayField: { type: 'Array', required: true, defaultValue: [1, 2, 3] },
169+
objectField: { type: 'Object', required: true, defaultValue: { foo: 'bar' } },
170+
ACL: { type: 'ACL' }
171+
});
172+
const object = new Parse.Object('SchemaFieldTest');
173+
await object.save();
174+
const json = object.toJSON();
175+
delete json.createdAt;
176+
delete json.updatedAt;
177+
delete json.objectId;
178+
179+
const expected = {
180+
defaultFieldString: 'hello',
181+
stringField: 'world',
182+
numberField: 10,
183+
booleanField: false,
184+
dateField: { __type: 'Date', iso: '2000-01-01T00:00:00.000Z' },
185+
dateStringField: { __type: 'Date', iso: '2000-01-01T00:00:00.000Z' },
186+
fileField: file.toJSON(),
187+
geoPointField: point.toJSON(),
188+
polygonField: {
189+
__type: 'Polygon',
190+
coordinates: [ [ 0, 0 ], [ 0, 1 ], [ 1, 1 ], [ 1, 0 ], [ 0, 0 ] ]
191+
},
192+
arrayField: [ 1, 2, 3 ],
193+
objectField: { foo: 'bar' },
194+
};
195+
assert.deepEqual(json, expected);
196+
});
197+
107198
it('save class level permissions', async () => {
108199
const clp = {
109200
get: { requiresAuthentication: true },

0 commit comments

Comments
 (0)