Skip to content

Commit

Permalink
added a validation step to evaluation and fix invalid views
Browse files Browse the repository at this point in the history
  • Loading branch information
Yngwarr committed Feb 26, 2024
1 parent 54c34ea commit b21136b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
1 change: 1 addition & 0 deletions sof-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"dependencies": {
"ajv": "^8.12.0",
"ajv-cli": "^5.0.0",
"awesome-ajv-errors": "^5.1.0",
"fhirpath": "3.9.0",
"prettier": "^3.0.3"
},
Expand Down
9 changes: 8 additions & 1 deletion sof-js/src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { prettify } from 'awesome-ajv-errors'
import { fhirpath_evaluate } from './path.js'
import {errors as verrors} from './validate.js'
import {errors as verrors, validate} from './validate.js'


export let errors = verrors
Expand Down Expand Up @@ -273,6 +274,12 @@ export function evaluate(def, node) {
return evaluate(def, [node])
}

const validation = validate(def);
if ((validation.errors || []).length > 0) {
throw new Error("Incorrect view definition:\n"
.concat(prettify(validation, { data: def })));
}

const normal_def = normalize(structuredClone(def));

// console.log("======= NORM =========")
Expand Down
29 changes: 23 additions & 6 deletions sof-js/src/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ let viewdef_schema = {
additionalProperties: false,
properties: {
title: string,
status: string,
resource: identifier,
constant: {
type: array,
Expand Down Expand Up @@ -66,8 +67,20 @@ let viewdef_schema = {
]
}
},
select: $ref('select'),
where: fhirpath_string
select: $ref('select'),
where:
{
type: array,
items: {
type: object,
required: ["path"],
additionalProperties: false,
properties: {
path: fhirpath_string,
description: string
}
}
}
},
$defs: {
tag: {
Expand Down Expand Up @@ -116,17 +129,21 @@ let viewdef_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);
validate_schema(viewdef);
return validate_schema;
}

export function errors(viewdef) {
let validate_schema = ajv.compile(viewdef_schema)
validate_schema(viewdef)
return validate_schema.errors
const validate_schema = validate(viewdef);
return validate_schema.errors;
}

// console.log(errors({select: [{forEach: 'name'}]}))

0 comments on commit b21136b

Please sign in to comment.