Skip to content

Commit 30d2546

Browse files
committed
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?
1 parent 41db1dc commit 30d2546

File tree

3 files changed

+451
-546
lines changed

3 files changed

+451
-546
lines changed

integration/test/ParseSchemaTest.js

Lines changed: 76 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,80 @@ 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('set multiple required and default values', async () => {
122+
const pointer = new TestObject();
123+
await pointer.save();
124+
const point = new Parse.GeoPoint(44.0, -11.0);
125+
const polygon = new Parse.Polygon([[0,0], [0,1], [1,1], [1,0]]);
126+
const file = new Parse.File('parse-server-logo', { base64: 'ParseA==' });
127+
await file.save();
128+
const testSchema = new Parse.Schema('SchemaFieldTest');
129+
130+
testSchema
131+
.addField('defaultFieldString', 'String', { required: true, defaultValue: 'hello' })
132+
.addString('stringField', { required: true, defaultValue: 'world' })
133+
.addNumber('numberField', { required: true, defaultValue: 10 })
134+
.addBoolean('booleanField', { required: true, defaultValue: false })
135+
// .addDate('dateField', { required: true, defaultValue: new Date('January 1, 2000 00:00:00') })
136+
.addFile('fileField', { required: true, defaultValue: file })
137+
.addGeoPoint('geoPointField', { required: true, defaultValue: point })
138+
.addPolygon('polygonField', { required: true, defaultValue: polygon })
139+
.addArray('arrayField', { required: true, defaultValue: [1, 2, 3] })
140+
.addObject('objectField', { required: true, defaultValue: { foo: 'bar' } })
141+
// .addPointer('pointerField', 'TestObject', { required: true, defaultValue: pointer })
142+
143+
const schema = await testSchema.save();
144+
assert.deepEqual(schema.fields, {
145+
objectId: { type: 'String' },
146+
updatedAt: { type: 'Date' },
147+
createdAt: { type: 'Date' },
148+
defaultFieldString: { type: 'String', required: true, defaultValue: 'hello' },
149+
stringField: { type: 'String', required: true, defaultValue: 'world' },
150+
numberField: { type: 'Number', required: true, defaultValue: 10 },
151+
booleanField: { type: 'Boolean', required: true, defaultValue: false },
152+
fileField: { type: 'File', required: true, defaultValue: file.toJSON() },
153+
geoPointField: { type: 'GeoPoint', required: true, defaultValue: point.toJSON() },
154+
polygonField: { type: 'Polygon', required: true, defaultValue: polygon.toJSON() },
155+
arrayField: { type: 'Array', required: true, defaultValue: [1, 2, 3] },
156+
objectField: { type: 'Object', required: true, defaultValue: { foo: 'bar' } },
157+
ACL: { type: 'ACL' }
158+
});
159+
const object = new Parse.Object('SchemaFieldTest');
160+
await object.save();
161+
const json = object.toJSON();
162+
delete json.createdAt;
163+
delete json.updatedAt;
164+
delete json.objectId;
165+
166+
const expected = {
167+
defaultFieldString: 'hello',
168+
stringField: 'world',
169+
numberField: 10,
170+
booleanField: false,
171+
fileField: file.toJSON(),
172+
geoPointField: point.toJSON(),
173+
polygonField: {
174+
__type: 'Polygon',
175+
coordinates: [ [ 0, 0 ], [ 0, 1 ], [ 1, 1 ], [ 1, 0 ], [ 0, 0 ] ]
176+
},
177+
arrayField: [ 1, 2, 3 ],
178+
objectField: { foo: 'bar' },
179+
};
180+
assert.deepEqual(json, expected);
181+
});
182+
107183
it('save class level permissions', async () => {
108184
const clp = {
109185
get: { requiresAuthentication: true },

0 commit comments

Comments
 (0)