From 8eae7990dd8e187fd8b1349e71ea8aecd2b1f315 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexis=20M=C3=A9taireau?= Date: Mon, 21 Oct 2024 17:51:07 +0200 Subject: [PATCH] Add a `fieldInSchema` utility function. Check that fields are present in the schema before calling render --- umap/static/umap/js/modules/sync/updaters.js | 11 +++++++-- umap/static/umap/js/modules/utils.js | 14 ++++++++++++ umap/static/umap/unittests/utils.js | 24 ++++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/umap/static/umap/js/modules/sync/updaters.js b/umap/static/umap/js/modules/sync/updaters.js index 896aed06..a8327941 100644 --- a/umap/static/umap/js/modules/sync/updaters.js +++ b/umap/static/umap/js/modules/sync/updaters.js @@ -1,3 +1,5 @@ +import { fieldInSchema } from '../utils.js' + /** * Updaters are classes able to convert messages * received from other peers (or from the server) to changes on the map. @@ -42,9 +44,10 @@ class BaseUpdater { export class MapUpdater extends BaseUpdater { update({ key, value }) { - if (key !== 'numberOfConnectedPeers') { + if (fieldInSchema(key)){ this.updateObjectValue(this.map, key, value) } + this.map.render([key]) } } @@ -58,7 +61,11 @@ export class DataLayerUpdater extends BaseUpdater { update({ key, metadata, value }) { const datalayer = this.getDataLayerFromID(metadata.id) - this.updateObjectValue(datalayer, key, value) + if (fieldInSchema(key)) { + this.updateObjectValue(datalayer, key, value) + } else { + console.debug('Not applying update for datalayer because key is not in the schema', key) + } datalayer.render([key]) } } diff --git a/umap/static/umap/js/modules/utils.js b/umap/static/umap/js/modules/utils.js index 33aeb55e..694fba52 100644 --- a/umap/static/umap/js/modules/utils.js +++ b/umap/static/umap/js/modules/utils.js @@ -54,6 +54,20 @@ export function getImpactsFromSchema(fields, schema) { return Array.from(impacted) } +/** + * Check if a field exists in the schema. + * + * @param {string} field + * @param {object} schema + * @returns {boolean} + */ +export function fieldInSchema(field, schema) { + if (typeof field !== 'string') return false + field = field.replace('options.', '').split('.')[0] + schema = schema || U.SCHEMA + return schema[field] !== undefined +} + /** * Import DOM purify, and initialize it. * diff --git a/umap/static/umap/unittests/utils.js b/umap/static/umap/unittests/utils.js index abcf15c1..cc70bfee 100644 --- a/umap/static/umap/unittests/utils.js +++ b/umap/static/umap/unittests/utils.js @@ -747,6 +747,30 @@ describe('Utils', () => { }) }) + describe('#fieldInSchema', () => { + it('should return true if the field is in the schema', () => { + assert.equal(Utils.fieldInSchema('foo', { foo: {} }), true) + }) + it('should return false if the field is not in the schema', () => { + assert.equal(Utils.fieldInSchema('foo', { bar: {} }), false) + }) + it('should return false if the schema is not provided', () => { + assert.equal(Utils.fieldInSchema('foo', {}), false) + }) + it('should return false if the field is undefined', () => { + assert.equal(Utils.fieldInSchema(undefined, {}), false) + }) + // check that options. is removed + it('should remove options. from the field', () => { + assert.equal(Utils.fieldInSchema('options.foo', { foo: {} }), true) + }) + + // check that subfields are removed + it('should remove subfields from the field', () => { + assert.equal(Utils.fieldInSchema('options.foo.bar', { foo: { bar: {} } }), true) + }) + }) + describe('#parseNaiveDate', () => { it('should parse a date', () => { assert.equal(