Skip to content

Commit

Permalink
Add a fieldInSchema utility function.
Browse files Browse the repository at this point in the history
Check that fields are present in the schema before calling render
  • Loading branch information
almet committed Oct 22, 2024
1 parent 627357e commit e11484c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
11 changes: 9 additions & 2 deletions umap/static/umap/js/modules/sync/updaters.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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])
}
}
Expand All @@ -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])
}
}
Expand Down
14 changes: 14 additions & 0 deletions umap/static/umap/js/modules/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
24 changes: 24 additions & 0 deletions umap/static/umap/unittests/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit e11484c

Please sign in to comment.