Skip to content

Commit

Permalink
Extend schema validation of positions
Browse files Browse the repository at this point in the history
  • Loading branch information
nichtich committed Dec 14, 2023
1 parent f296385 commit 08aa3fa
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
23 changes: 19 additions & 4 deletions lib/schema-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ export class SchemaValidator {
}

validateSubfieldSchedule(subfields) {
// TODO: not covered by test suite
const errors = []
for (let code in subfields) {
const sf = subfields[code]
Expand All @@ -97,14 +96,30 @@ export class SchemaValidator {
.map(([key, code]) => ({ message: `code '${code.code}' must be '${key}' in codelist` }))
}

validatePositions(/*positions*/) {
return [] // TODO
validatePositions(positions) {
const errors = []
for (let pos in positions) {
const [start, end] = pos.split("-")
// TODO: check for overlap
const p = positions[pos]
if ("start" in p && p.start != start) {
errors.push({ message: `position start '${p.start}' must be '${start}'` })
}
if (end === undefined) {
if ("end" in p) {
errors.push({ message: `position should not have end '${p.end}'` })
}
} else if ("end" in p && p.end != end) {
errors.push({ message: `position end '${p.end}' must be '${end}'` })
}
}
return errors
}

validatePattern(pattern) {
if (pattern != null) {
try { new RegExp(pattern) } catch (e) {
return [{ message: `Invalid pattern '${pattern}'` }]
return [{ message: `invalid pattern '${pattern}'` }]
}
}
return []
Expand Down
31 changes: 27 additions & 4 deletions test/schema-suite.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,22 @@
},
{
"schema": {
"fields": {},
"fields": {
"p": {
"positions": {
"0-1": { "start": "1", "end": "3" },
"2": { "start": 2, "end": 2 }
}
}
},
"url": "x"
},
"errors": [ "must match format \"uri\"" ]
"errors": [
"must match format \"uri\"",
"position should not have end '2'",
"position start '1' must be '0'",
"position end '3' must be '1'"
]
},
{
"schema": {
Expand Down Expand Up @@ -75,7 +87,15 @@
"schema": {
"fields": {
"abc": {
"subfields": { "x": {} },
"subfields": {
"x": {
"code": "y",
"codes": {
"1": { "code": "2" }
},
"pattern": "("
}
},
"codes": {}
},
"number": {
Expand All @@ -85,7 +105,10 @@
},
"errors": [
"variable field must not have codes",
"Invalid pattern '['"
"subfield code 'y' must be 'x'",
"code '2' must be '1' in codelist",
"invalid pattern '('",
"invalid pattern '['"
]
}
]
Expand Down

0 comments on commit 08aa3fa

Please sign in to comment.