diff --git a/sof-js/src/index.js b/sof-js/src/index.js index c4c4138..120af03 100644 --- a/sof-js/src/index.js +++ b/sof-js/src/index.js @@ -266,12 +266,12 @@ export function get_columns(def) { return collect_columns([], normalize(structuredClone(def))); } -export function evaluate(def, node) { +export function evaluate(def, node, for_test = true) { if (!Array.isArray(node)) { return evaluate(def, [node]) } - const validation = validate(def); + const validation = validate(def, for_test); if ((validation.errors || []).length > 0) { throw new Error("Incorrect view definition:\n" .concat(JSON.stringify(validation.errors, null, 2))); diff --git a/sof-js/src/validate.js b/sof-js/src/validate.js index 727decd..dbe4484 100644 --- a/sof-js/src/validate.js +++ b/sof-js/src/validate.js @@ -21,113 +21,130 @@ function const_value(name, type = string) { }; } -let viewdef_schema = { - $id: "http://hl7.org/fhir/uv/sql-on-fhir/ViewDefinition", - title: "ViewDefinition", - description: "validate FHIR ViewDefinition schema", - type: object, - required: ["resource", "select"], - additionalProperties: false, - properties: { - title: string, - status: string, - resource: identifier, - constant: { - type: array, - minItems: 0, - items: { - allOf: [ - { - type: object, - required: ["name"], - properties: { name: string } - }, { - oneOf: [ - const_value("valueBase64Binary"), - const_value("valueBoolean", bool), - const_value("valueCanonical"), - const_value("valueCode"), - const_value("valueDate"), - const_value("valueDateTime"), - const_value("valueDecimal", { type: "number" }), - const_value("valueId"), - const_value("valueInstant"), - const_value("valueInteger", { type: "integer" }), - const_value("valueInteger64"), - const_value("valueOid"), - const_value("valueString"), - const_value("valuePositiveInt", { type: "integer", minimum: 1 }), - const_value("valueTime"), - const_value("valueUnsignedInt", { type: "integer", minimum: 0 }), - const_value("valueUri"), - const_value("valueUrl"), - const_value("valueUuid") - ] - } - ] - } - }, - select: $ref('select'), - where: - { - type: array, - items: { - type: object, - required: ["path"], - additionalProperties: false, - properties: { - path: fhirpath_string, - description: string + +function viewdef_schema(for_tests = false) { + let column_required_fields + let id = "http://hl7.org/fhir/uv/sql-on-fhir/ViewDefinition" + if (for_tests) { + column_required_fields = ["path", "name", "type"] + id = id + "_test" + } else { + column_required_fields = ["path", "name"] + } + + let schema = { + $id: id, + title: "ViewDefinition", + description: "validate FHIR ViewDefinition schema", + type: object, + required: ["resource", "select"], + additionalProperties: false, + properties: { + title: string, + status: string, + resourceType: string, + name: string, + resource: identifier, + constant: { + type: array, + minItems: 0, + items: { + allOf: [ + { + type: object, + required: ["name"], + properties: { name: string } + }, { + oneOf: [ + const_value("valueBase64Binary"), + const_value("valueBoolean", bool), + const_value("valueCanonical"), + const_value("valueCode"), + const_value("valueDate"), + const_value("valueDateTime"), + const_value("valueDecimal", { type: "number" }), + const_value("valueId"), + const_value("valueInstant"), + const_value("valueInteger", { type: "integer" }), + const_value("valueInteger64"), + const_value("valueOid"), + const_value("valueString"), + const_value("valuePositiveInt", { type: "integer", minimum: 1 }), + const_value("valueTime"), + const_value("valueUnsignedInt", { type: "integer", minimum: 0 }), + const_value("valueUri"), + const_value("valueUrl"), + const_value("valueUuid") + ] + } + ] } - } - } - }, - $defs: { - tag: { - type: array, - items: { - type: object, - additionalProperties: false, - properties: { - name: string, - value: string + }, + select: $ref('select'), + where: + { + type: array, + items: { + type: object, + required: ["path"], + additionalProperties: false, + properties: { + path: fhirpath_string, + description: string + } } } }, - column: { - type: array, - minItems: 1, - items: { - type: object, - required: ["path", "name", "type"], - additionalProperties: false, - properties: { - path: fhirpath_string, - name: identifier, - collection: bool, - description: string, - type: string, - tag: $ref('tag') + $defs: { + tag: { + type: array, + items: { + type: object, + additionalProperties: false, + properties: { + name: string, + value: string + } } - } - }, - select: { - type: array, - minItems: 1, - items: { - type: object, - additionalProperties: false, - properties: { - column: $ref('column'), - unionAll: $ref('select'), - forEach: fhirpath_string, - forEachOrNull: fhirpath_string, - select: $ref('select') + }, + column: { + type: array, + minItems: 1, + items: { + type: object, + required: column_required_fields, + additionalProperties: false, + properties: { + path: fhirpath_string, + name: identifier, + collection: bool, + description: string, + type: string, + tag: $ref('tag') + } + } + }, + select: { + type: array, + minItems: 1, + items: { + type: object, + additionalProperties: false, + properties: { + column: $ref('column'), + unionAll: $ref('select'), + forEach: fhirpath_string, + forEachOrNull: fhirpath_string, + select: $ref('select') + } } } } } -}; + + return schema +} + const ajv = new Ajv({ allErrors: true }) function validate_fhirpath(path) { @@ -135,8 +152,16 @@ function validate_fhirpath(path) { } ajv.addFormat('fhirpath-expression',{type: 'string', validate: validate_fhirpath}) -export function validate(viewdef) { - const validate_schema = ajv.compile(viewdef_schema); +let schema_test = viewdef_schema(true); +let schema_playground = viewdef_schema(false); + +export function validate(viewdef, for_tests = true) { + let validate_schema + if (for_tests) { + validate_schema = ajv.compile(schema_test); + } else { + validate_schema = ajv.compile(schema_playground); + } validate_schema(viewdef); return validate_schema; } diff --git a/test_report/src/Playground.svelte b/test_report/src/Playground.svelte index d65ad34..dba3f33 100644 --- a/test_report/src/Playground.svelte +++ b/test_report/src/Playground.svelte @@ -117,7 +117,7 @@ try { error = null; eval('v = ' + viewdef) - result = tabelize(v, evaluate(v, data)); + result = tabelize(v, evaluate(v, data, false)); /* tabelize(result) */ } catch(e) { error = e.toString();