Skip to content

Commit

Permalink
Add test to show that you can't use patternProperties in schema (#4951
Browse files Browse the repository at this point in the history
)

* Add test for getting field defined in definitions

* Add test

* Don't check field name with regex if it's inside patternProperties

* Improve test

* Remove lines that break test and move to rx-schema tests

---------

Co-authored-by: Adam <[email protected]>
  • Loading branch information
adam-lynch and Adam authored Oct 13, 2023
1 parent 953ea0d commit 0545ae2
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/plugins/dev-mode/check-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ export function validateFieldsDeep(rxJsonSchema: RxJsonSchema<any>): true {
if (
typeof fieldName === 'string' &&
typeof schemaObj === 'object' &&
!Array.isArray(schemaObj)
!Array.isArray(schemaObj) &&
path.split('.').pop() !== 'patternProperties'
) checkFieldNameRegex(fieldName);

// 'item' only allowed it type=='array'
Expand Down
96 changes: 96 additions & 0 deletions test/unit/rx-schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1072,6 +1072,102 @@ config.parallel('rx-schema.test.js', () => {
}
});

db.destroy();
});
it('#4951 patternProperties are allowed', async () => {
/**
* Dexie.js does not support boolean indexes,
* see docs-src/rx-storage-dexie.md
*/
if (config.storage.name.includes('dexie')) {
return;
}

const db = await createRxDatabase({
name: randomCouchString(10),
storage: config.storage.getStorage()
});

const mySchema = {
'keyCompression': false,
'version': 0,
'primaryKey': 'passportId',
'type': 'object',
'properties': {
passportId: {
type: 'string',
maxLength: 100
},
firstName: {
type: 'string'
},
lastName: {
type: 'string'
},
age: {
type: 'integer',
minimum: 0,
maximum: 150
},
tags: {
type: 'object',
patternProperties: {
'.*': {
type: 'object',
properties: {
created: {
type: 'integer',
},
name: {
type: 'string',
},
},
required: ['created', 'name'],
}
},
},
},
};
const collections = await db.addCollections({
test: {
schema: mySchema
}
});

const tags = {
hello: {
created: 1,
name: 'hello',
},
world: {
created: 2,
name: 'world',
}
};

// insert a document
await collections.test.insert({
passportId: 'foobar',
firstName: 'Bob',
lastName: 'Kelso',
age: 56,
tags,
});

const myDocument = await collections.test
.findOne()
.exec();

assert.deepStrictEqual(myDocument.toJSON().tags.hello, tags.hello, 'myDocument.toJSON().tags.hello');
assert.deepStrictEqual(myDocument.toJSON().tags.world, tags.world, 'myDocument.toJSON().tags.world');
assert.deepStrictEqual(Object.keys(myDocument.toJSON().tags), Object.keys(tags), 'Object.keys(myDocument.toJSON().tags)');

assert.deepStrictEqual(myDocument.get('tags').hello, tags.hello, 'myDocument.get(\'tags\').hello');
assert.deepStrictEqual(myDocument.get('tags').world, tags.world, 'myDocument.get(\'tags\').world');

assert.deepStrictEqual(myDocument.tags.hello, tags.hello, 'myDocument.tags.hello');
assert.deepStrictEqual(myDocument.tags.world, tags.world, 'myDocument.tags.world');

db.destroy();
});
});
Expand Down

0 comments on commit 0545ae2

Please sign in to comment.