Skip to content

Commit

Permalink
Merge branch 'main' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
nichtich committed Nov 28, 2023
2 parents ba31324 + b3af9c4 commit 27b398c
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 33 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ An empty string schema argument uses the empty schema. Combining -n and -v emits
parsed records. Validation options (separable with any of [ ,|+]):
ignore_unknown_fields
allow_deprecated
ignore_subfields
ignore_unknown_subfields
ignore_values
Expand Down
21 changes: 1 addition & 20 deletions lib/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export class Validator {
constructor(schema, options={}) {
this.codelists = schema.codelists || {}
this.schedule = new FieldSchedule(schema)
this.deprecated = new FieldSchedule({ ...schema, fields: schema["deprecated-fields"] || {} })
this.options = options
}

Expand All @@ -16,7 +15,6 @@ export class Validator {

const { schedule } = this
const visitor = new ScheduleVisitor(schedule)
// const deprecatedVisitor = new ScheduleVisitor(deprecated)
const errors = []

for (let field of record) {
Expand All @@ -35,7 +33,6 @@ export class Validator {
? {message: `Unknown field '${key}/${occurrence}'.`, key, occurrence}
: {message: `Unknown field '${key}'.`, key})
}
// TODO: support deprecated-fields
}

const missing = visitor.missing().map(id => ({
Expand All @@ -51,7 +48,6 @@ export class Validator {
Validator.options = {
ignore_unknown_fields: true,
ignore_subfields: true,
// allow_deprecated: true,
ignore_unknown_subfields: true,
// check_subfield_order: true,
ignore_values: true,
Expand Down Expand Up @@ -136,7 +132,7 @@ export const validatePattern = (value, pattern) => {

// See https://format.gbv.de/schema/avram/specification#validation-with-codelist
export const validateCode = (value, definition, options = {}) => {
const { ignore_codes, ignore_unknown_codelists, allow_deprecated, codelists } = options
const { ignore_codes, ignore_unknown_codelists, codelists } = options

var { codes } = definition
if (ignore_codes || !codes) return []
Expand All @@ -154,21 +150,6 @@ export const validateCode = (value, definition, options = {}) => {
// value found as code
if (value in codes) return []

// maybe the value is a deprecated code
if (allow_deprecated) {
codes = definition["deprecated-codes"]
if (typeof codes === "string") {
// This code is duplicated above, maybe merge into function?
if (codelists && codes in codelists) {
codes = codelists[codes]
} else {
return ignore_unknown_codelists ? [] :
[{ message: `Unknown codelist '${codes}'.`, value: codes }]
}
}
if (codes && value in codes) return []
}

// TODO: we may include information about referenced codelist
return [{message: `Value '${value}' is not defined in codelist.`, value}]
}
Expand Down
19 changes: 7 additions & 12 deletions test/suite/codes.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@
},
"fields": {
"lang": {
"codes": "languages",
"deprecated-codes": { "deu": { } }
"codes": "languages"
},
"bool": {
"codes": { "yes": {}, "no": {} },
"deprecated-codes": "not-found"
"codes": { "yes": {}, "no": {} }
},
"wtf": {
"codes": "xy"
Expand Down Expand Up @@ -45,18 +43,15 @@
],
"options": ["ignore_codes"]
},{
"description": "allow_deprecated",
"record": [
{ "key": "lang", "value": "deu" }
],
"options": ["allow_deprecated"]
},{
"description": "ignore_unknown_codelists & allow_deprecated",
"description": "ignore_unknown_codelists",
"record": [
{ "key": "bool", "value": "y" },
{ "key": "wtf", "value": "xy" }
],
"options": ["ignore_unknown_codelists", "allow_deprecated"]
"options": ["ignore_unknown_codelists"],
"errors": [
{ "message": "Value 'y' is not defined in codelist.", "value": "y" }
]
}
]
}
Expand Down

0 comments on commit 27b398c

Please sign in to comment.