diff --git a/coding-scheme.interface.ts b/coding-scheme.interface.ts index c2002f2..145150c 100644 --- a/coding-scheme.interface.ts +++ b/coding-scheme.interface.ts @@ -62,6 +62,10 @@ export type CodingSchemeProblemType = | 'SOURCE_MISSING' | 'INVALID_SOURCE' | 'RULE_PARAMETER_COUNT_MISMATCH' + | 'RULE_REGEX_INVALID' + | 'RULE_PARAMETER_INVALID' + | 'RULE_NUMERIC_RANGE_INVALID' + | 'RULESET_VALUE_ARRAY_POS_INVALID' | 'MORE_THAN_ONE_SOURCE' | 'ONLY_ONE_SOURCE' | 'VALUE_COPY_NOT_FROM_BASE'; diff --git a/coding-scheme.schema.json b/coding-scheme.schema.json index ebbd2de..ba44273 100644 --- a/coding-scheme.schema.json +++ b/coding-scheme.schema.json @@ -19,12 +19,12 @@ "id": { "type": "string", "description": "Identifier for the variable. In case of a scheme for a base variable, this id is identical with the source variable's id. All ids of a response scheme should be unique.", - "pattern": "^[0-9a-zA-Z_]+$" + "pattern": "^[0-9a-zA-Z_-]+$" }, "alias": { "type": "string", "description": "Alternative identifier for the variable. In case of a scheme for a base variable, this id is identical with the source variable's alternative id. All alternative ids of a response scheme should be unique.", - "pattern": "^[0-9a-zA-Z_]+$" + "pattern": "^[0-9a-zA-Z_-]+$" }, "label": { "type": "string", @@ -80,7 +80,7 @@ "type": "array", "items": { "type": "string", - "pattern": "^[0-9a-zA-Z_]+$" + "pattern": "^[0-9a-zA-Z_-]+$" } }, "processing": { diff --git a/coding-scheme.ts b/coding-scheme.ts index 3482320..78d5665 100644 --- a/coding-scheme.ts +++ b/coding-scheme.ts @@ -81,24 +81,26 @@ export class CodingScheme { const givenCodings = Array.isArray(transformedScheme) ? transformedScheme : transformedScheme.variableCodings || []; + // eslint-disable-next-line @typescript-eslint/no-explicit-any givenCodings.forEach((c: any) => { - this.variableCodings.forEach(vc => { - vc.codes?.forEach(code => { - if (code.id === null) code.id = 'INVALID'; - }); - }); + let vc: VariableCodingData; if (codingSchemeMajorVersion < 3) { - this.variableCodings.push( - CodingScheme.getCodeVersionLessThan3(c) - ); + vc = CodingScheme.getCodeVersionLessThan3(c); } else { - this.variableCodings.push(c); + vc = c; } + CodingScheme.applyLegacyFixes(vc); + this.variableCodings.push(vc); + }); + this.variableCodings.forEach(vc => { + vc.codes?.forEach(code => { + if (code.id === null) code.id = 'INVALID'; + }); }); } - // eslint-disable-next-line @typescript-eslint/no-explicit-any private static getCodeVersionLessThan3( + // eslint-disable-next-line @typescript-eslint/no-explicit-any givenCoding: any ): VariableCodingData { const codes: CodeData[] = []; @@ -166,8 +168,8 @@ export class CodingScheme { // eslint-disable-next-line @typescript-eslint/no-explicit-any givenCoding.codes.forEach((code: any) => { if (code.ruleSets) { - // eslint-disable-next-line @typescript-eslint/no-explicit-any const elseRule = code.ruleSets.find( + // eslint-disable-next-line @typescript-eslint/no-explicit-any (rs: any) => !!rs.rules.find((r: any) => r.method === 'ELSE') ); if (elseRule) { @@ -205,7 +207,33 @@ export class CodingScheme { return newCoding; } + private static applyLegacyFixes(vc: VariableCodingData): void { + if ((vc.codeModel as unknown as string) === 'NONE') { + const hasRules = vc.codes?.some(c => (c.ruleSets && c.ruleSets.length > 0)); + const hasManualInstruction = !!vc.manualInstruction || vc.codes?.some(c => !!c.manualInstruction); + if (vc.sourceType === 'MANUAL') { + vc.codeModel = 'MANUAL_ONLY'; + } else if (hasRules && hasManualInstruction) { + vc.codeModel = 'MANUAL_AND_RULES'; + } else if (hasRules) { + vc.codeModel = 'RULES_ONLY'; + } else if (hasManualInstruction) { + vc.codeModel = 'MANUAL_ONLY'; + } else { + vc.codeModel = 'RULES_ONLY'; + } + } + vc.codes?.forEach(c => { + if (c.ruleSets && Array.isArray(c.ruleSets)) { + c.ruleSets.forEach(rs => { + if ((rs.valueArrayPos as unknown as number) === -1) rs.valueArrayPos = 'ANY'; + }); + } + }); + } + static checkVersion( + // eslint-disable-next-line @typescript-eslint/no-explicit-any givenScheme: any ): 'OK' | 'MAJOR_LESS' | 'MAJOR_GREATER' | 'MINOR_GREATER' { const transformedScheme = diff --git a/package_npm.json b/package_npm.json index 5776110..7f19297 100644 --- a/package_npm.json +++ b/package_npm.json @@ -1,6 +1,6 @@ { "name": "@iqbspecs/coding-scheme", - "version": "3.3.1", + "version": "3.4", "author": "IQB - Institut zur Qualitätsentwicklung im Bildungswesen", "license": "CC0 1.0", "description": "Data interface: coding-scheme", diff --git a/test/test_invalid/derive-sources-invalid-pattern.json b/test/test_valid/derive-sources-with-hyphen.json similarity index 100% rename from test/test_invalid/derive-sources-invalid-pattern.json rename to test/test_valid/derive-sources-with-hyphen.json diff --git a/test/test_invalid/invalid-id-with-special-chars.json b/test/test_valid/id-with-hyphen.json similarity index 100% rename from test/test_invalid/invalid-id-with-special-chars.json rename to test/test_valid/id-with-hyphen.json diff --git a/test/test_valid/valid-id-patterns.json b/test/test_valid/valid-id-patterns.json index 2b33afe..0e9c988 100644 --- a/test/test_valid/valid-id-patterns.json +++ b/test/test_valid/valid-id-patterns.json @@ -25,6 +25,15 @@ { "id": "a", "sourceType": "BASE" + }, + { + "id": "var-with-hyphen", + "sourceType": "BASE" + }, + { + "id": "v-123-abc", + "alias": "alias-with-hyphen-123", + "sourceType": "BASE" } ] }