Skip to content

Commit

Permalink
Fix validation rule invalidPosition
Browse files Browse the repository at this point in the history
  • Loading branch information
nichtich committed Dec 22, 2023
1 parent 2d7b2e7 commit 06a2afb
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 19 deletions.
1 change: 0 additions & 1 deletion lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ class Options {
const arg = args.shift()
if (arg === "--") break

// TODO: combine letters
const match = arg.match(/^(-\w|--\w[\w-]*)$/)
if (match) {
let name = arg.replace(/^-+/, "")
Expand Down
22 changes: 12 additions & 10 deletions lib/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ Validator.options = {
undefinedCodelist: false,
countRecord: false,
countField: false,
// TODO:
// countSubfield: false,
// countCode: false,
// externalRule: false,
Expand Down Expand Up @@ -235,27 +234,30 @@ export const validateCode = (value, definition, options = {}, isIndicator = fals
export const validatePositions = (value, positions, options = {}) => {
if (!positions || !options.invalidPosition) return []

value = Buffer.from(value, "utf-8")
const chars = Buffer.from(value, "utf-8")

const errors = Object.entries(positions)
.map(([range, definition]) => {
if (definition.pattern || definition.codes) {
const [start, end] = range.split("-").map(n => 1*n)

// TODO: document replacement characters if range is within a unicode character
// TODO: error if value to short for positions

if (start >= chars.length || end >= chars.length) {
return {
message: `Position ${range} does not exist`,
error: "invalidPosition",
value,
}
}

const slice = end
// utf-8 indexed by byte position
? value.slice(start, end+1).toString()
? chars.slice(start, end+1).toString()
// one byte as unicode codepoint
: (start < value.length ? String.fromCodePoint(value[start]) : "")
: String.fromCodePoint(chars[start])

// TODO: error code invalidPosition

if (slice !== "") {
return validateValue(slice, definition, options)
}
return validateValue(slice, definition, options)
}
})
.filter(Boolean)
Expand Down
13 changes: 11 additions & 2 deletions test/suite/positions.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,19 @@
"tests": [
{
"record": [
{ "tag": "x", "value": "a" },
{ "tag": "x", "value": "axy" },
{ "tag": "x", "value": "zxy" },
{ "tag": "x", "value": "axyz" }
]
},
{
"record": [
{ "tag": "x", "value": "a" }
],
"errors": [{
"error": "invalidPosition",
"message": "Position 01-2 does not exist",
"value": "a"
}]
}
]
}
Expand Down
17 changes: 11 additions & 6 deletions test/suite/validate-values.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"_": {
"repeatable": true,
"positions": {
"00-01": { "pattern": "[a-z]" },
"00": { "pattern": "[a-z]" },
"01-02": { "pattern": "[a-z0-9]" }
}
}
Expand All @@ -61,20 +61,25 @@
"tests":[
{
"record": [
{"tag":"_","value":""},
{"tag":"_","value":"ab"},
{"tag":"_","value":"abc"},
{"tag":"_","value":"ab9"}
{"tag":"_","value":"ab0"},
{"tag":"_","value":"xx9z"}
]
},
{
"record": [{"tag":"_","value":"9"}],
"record": [
{"tag":"_","value":"9a"}
],
"errors": [
{
"message": "Value '9' does not match regex pattern '[a-z]'.",
"pattern": "[a-z]",
"error": "patternMismatch",
"value": "9"
},
{
"message": "Position 01-02 does not exist",
"value": "9a",
"error": "invalidPosition"
}
]
}
Expand Down

0 comments on commit 06a2afb

Please sign in to comment.