Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
johngrimes committed Mar 19, 2024
2 parents 6234d99 + 7f498da commit fd0a17a
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 103 deletions.
4 changes: 2 additions & 2 deletions sof-js/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down
225 changes: 125 additions & 100 deletions sof-js/src/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,122 +21,147 @@ 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) {
return fhirpath_validate(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;
}
Expand Down
2 changes: 1 addition & 1 deletion test_report/src/Playground.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit fd0a17a

Please sign in to comment.