Skip to content

Commit

Permalink
Merge pull request #4049 from open-formulieren/issue/4038-filtering-f…
Browse files Browse the repository at this point in the history
…or-geo-components

Support filtering for GeoJSON-compatible schema properties
  • Loading branch information
sergei-maertens authored Mar 22, 2024
2 parents 7138308 + 60bad42 commit 7983404
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,34 @@ const FORMAT_TYPE_MAP = {
* @returns {Object} - The JSON Schema
*/
const asJsonSchema = (variable, components) => {
// Figure out if the component is a file component (special case)
const componentDefinition = components[variable.key];
if (componentDefinition && componentDefinition.type === 'file') {
// If it is, and it has multiple == True, then type is array
if (componentDefinition.multiple)
return {type: 'array', items: {type: 'string', format: 'uri'}};
// Otherwise it's string (URL of the document)
return {type: 'string', format: 'uri'};
// Special handling for component types.
if (variable.source === 'component') {
const componentDefinition = components[variable.key];

switch (componentDefinition.type) {
case 'file': {
const {multiple = false} = componentDefinition;
const uriSchema = {type: 'string', format: 'uri'};
// If the component allows multiple files, the type is array, otherwise it's
// just a single string.
return multiple ? {type: 'array', items: uriSchema} : uriSchema;
}
case 'map': {
// TODO: in the future we could use `const: 'Point'` for the `type` key to be
// more strict.
return {
type: 'object',
properties: {
type: {
type: 'string',
},
},
};
}
}
}

// default behaviour - map the variable data type to a json-schema type.
if (VARIABLE_TYPE_MAP.hasOwnProperty(variable.dataType))
return {type: VARIABLE_TYPE_MAP[variable.dataType]};
return {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import {EMPTY_VARIABLE} from 'components/admin/form_design/variables/constants';

import {asJsonSchema} from './utils';

test('JSON schema for single file upload component', () => {
const component = {
type: 'file',
key: 'file',
label: 'File',
multiple: false,
};
const variable = {
...EMPTY_VARIABLE,
source: 'component',
key: 'file',
dataType: 'array',
};

const schema = asJsonSchema(variable, {[component.key]: component});

expect(schema).toEqual({
type: 'string',
format: 'uri',
});
});

test('JSON schema for multiple file uploads component', () => {
const component = {
type: 'file',
key: 'file',
label: 'File',
multiple: true,
};
const variable = {
...EMPTY_VARIABLE,
source: 'component',
key: 'file',
dataType: 'array',
};

const schema = asJsonSchema(variable, {[component.key]: component});

expect(schema).toEqual({
type: 'array',
items: {
type: 'string',
format: 'uri',
},
});
});

test('JSON schema for map components', () => {
const component = {
type: 'map',
key: 'map',
label: 'Point coordinates',
};
const variable = {
...EMPTY_VARIABLE,
source: 'component',
key: 'map',
dataType: 'array',
};

const schema = asJsonSchema(variable, {[component.key]: component});

// we could be stricter, but we basically expect GeoJSON at the other end, which is
// quite complex.
expect(schema).toEqual({
type: 'object',
properties: {
type: {
type: 'string',
},
},
});
});

0 comments on commit 7983404

Please sign in to comment.