Skip to content

Commit

Permalink
feat(validations): more verbose validation error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
adlerfaulkner committed Nov 3, 2023
1 parent 7a42813 commit 09b8b2d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@comake/skl-js-engine",
"version": "0.16.2",
"version": "0.16.3",
"description": "Standard Knowledge Language Javascript Engine",
"keywords": [
"skl",
Expand Down
45 changes: 41 additions & 4 deletions src/SklEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,10 @@ export class SKLEngine {
};
const report = await this.convertToQuadsAndValidateAgainstShape(entitiesOfType, nounSchemaWithTarget);
if (!report.conforms) {
throw new Error(`An entity does not conform to the ${currentNoun['@id']} schema.`);
this.throwValidationReportError(
report,
`An entity does not conform to the ${currentNoun['@id']} schema.`,
);
}
}
}
Expand Down Expand Up @@ -225,7 +228,10 @@ export class SKLEngine {
};
const report = await this.convertToQuadsAndValidateAgainstShape(entity, nounSchemaWithTarget);
if (!report.conforms) {
throw new Error(`Entity ${entity['@id']} does not conform to the ${currentNoun['@id']} schema.`);
this.throwValidationReportError(
report,
`Entity ${entity['@id']} does not conform to the ${currentNoun['@id']} schema.`,
);
}
}
}
Expand Down Expand Up @@ -281,7 +287,10 @@ export class SKLEngine {
const attributesWithId = { ...attributes, '@id': id };
const report = await this.convertToQuadsAndValidateAgainstShape(attributesWithId, nounSchemaWithTarget);
if (!report.conforms) {
throw new Error(`Entity ${id} does not conform to the ${currentNoun['@id']} schema.`);
this.throwValidationReportError(
report,
`Entity ${id} does not conform to the ${currentNoun['@id']} schema.`,
);
}
}
}
Expand Down Expand Up @@ -835,7 +844,10 @@ export class SKLEngine {
const parametersSchema = verb[SKL.parameters] as NodeObject;
const report = await this.convertToQuadsAndValidateAgainstShape(verbParamsAsJsonLd, parametersSchema);
if (!report.conforms) {
throw new Error(`${getValueIfDefined(verb[RDFS.label])} parameters do not conform to the schema`);
this.throwValidationReportError(
report,
`${getValueIfDefined(verb[RDFS.label])} parameters do not conform to the schema`,
);
}
}

Expand Down Expand Up @@ -1076,4 +1088,29 @@ export class SKLEngine {
// TODO add support for remote sources
throw new Error(`Failed to get data from source ${source}`);
}

private throwValidationReportError(report: ValidationReport, errorMessage: string): void {
const reportMessages = this.validationReportToMessages(report);
throw new Error(
`${errorMessage}\n\n${reportMessages.join('\n')}`,
);
}

private validationReportToMessages(report: ValidationReport): string[] {
const reportMessages = [];
for (const result of report.results) {
const pathValue = result.path?.value;
if (result.message.length === 0) {
const message = `${pathValue}: Invalid due to ${result.sourceConstraintComponent?.value}`;
reportMessages.push(message);
} else {
const resultMessages = result.message
.map((message): string => `${message.value}`)
.join(', ');
const message = `${pathValue}: ${resultMessages}`;
reportMessages.push(message);
}
}
return reportMessages;
}
}
2 changes: 1 addition & 1 deletion test/deploy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"main": "./dist/index.js",
"dependencies": {
"@comake/skl-js-engine": "file:./comake-skl-js-engine-0.16.2.tgz",
"@comake/skl-js-engine": "file:./comake-skl-js-engine-0.16.3.tgz",
"jsonld": "^6.0.0"
},
"devDependencies": {
Expand Down

0 comments on commit 09b8b2d

Please sign in to comment.