Merge pull request #218 from appy-one/fix/217-schema-validation-updates-on-higher-path

Fix schema validation of update on higher path
This commit is contained in:
Ewout Stortenbeker 2023-04-24 22:53:34 +02:00 committed by GitHub
commit 9efd8a84cd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 1 deletions

View file

@ -2281,7 +2281,7 @@ export class Storage extends SimpleEventEmitter {
pathInfo.isOnTrailOf(s.path), //pathInfo.equals(s.path) || pathInfo.isAncestorOf(s.path)
).every(s => {
if (pathInfo.isDescendantOf(s.path)) {
// Given check path is a descendant of this schema definition's path
// Given check path is a descendant of this schema definition's path
const ancestorPath = PathInfo.fillVariables(s.path, path);
const trailKeys = pathInfo.keys.slice(PathInfo.getPathKeys(s.path).length);
result = s.schema.check(ancestorPath, value, options.updates, trailKeys);
@ -2290,6 +2290,10 @@ export class Storage extends SimpleEventEmitter {
// Given check path is on schema definition's path or on a higher path
const trailKeys = PathInfo.getPathKeys(s.path).slice(pathInfo.keys.length);
if (options.updates === true && trailKeys.length > 0 && !(trailKeys[0] in value)) {
// Fixes #217: this update on a higher path does not affect any data at schema's target path
return result.ok;
}
const partial = options.updates === true && trailKeys.length === 0;
const check = (path: string, value: any, trailKeys: Array<string|number>): ISchemaCheckResult => {
if (trailKeys.length === 0) {

View file

@ -96,6 +96,10 @@ describe('schema', () => {
result = await db.schema.check('clients/client1', { unknown: null }, true);
expect(result).toEqual({ ok: true });
// Test updating a higher path that does not have a schema set (#217)
result = await db.schema.check('', { test: 'Test' }, true);
expect(result).toEqual({ ok: true });
// Try using classnames & regular expressions
const emailRegex = /[a-z.\-_]+@(?:[a-z\-_]+\.){1,}[a-z]{2,}$/i;
const clientSchema2 = {