Skip to content

Commit

Permalink
Store ajv errors in ValidationError object (#26)
Browse files Browse the repository at this point in the history
* feat: automatically add AJV errors to ValidationError.cause

* docs(changeset): Automatically add AJV errors to ValidationError.cause.

* fix: GH action permissions
  • Loading branch information
savvyintegrations authored Jun 6, 2024
1 parent 8514089 commit 41ed7c6
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/yellow-baboons-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nrfcloud/ts-json-schema-transformer": patch
---

Automatically add AJV errors to ValidationError.cause.
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
branches:
- main
concurrency: ${{ github.workflow }}-${{ github.ref }}
permissions: write-all
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/slacknotify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:

env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }}

permissions: write-all
jobs:
slackNotification:
if: false
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/stop-no-merge-labels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Block merging PRs with 'no-merge' label
on:
pull_request:
types: [synchronize, opened, reopened, labeled, unlabeled]

permissions: write-all
jobs:
no-merge:
if: ${{ contains(github.event.*.labels.*.name, 'no-merge') }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ on:
pull_request:
branches: [main]
types: [opened, synchronize]

permissions: write-all
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref_name }}
cancel-in-progress: true
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ export class ValidationError extends Error {
*/
export function validationAssertion(validator: ValidateFunction<unknown>, obj: unknown) {
if (!validator(obj)) {
throw new ValidationError(`Validation error: ${validator.errors?.map(error => JSON.stringify(error)).join(", ")}`);
throw new ValidationError(`Validation error: ${validator.errors?.map(error => JSON.stringify(error)).join(", ")}`, {
cause: validator.errors,
});
}
return obj;
}
17 changes: 12 additions & 5 deletions tests/src/assert.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,20 @@ describe("Simple Assert Test", () => {
});

it("should throw ValidationError object", () => {
let err;
try {
assertValid<SimpleType>("2021-01-01T00:00:00Z");
} catch (error) {
err = error;
} finally {
expect(err).toBeInstanceOf(ValidationError);
} catch (e) {
const error = e as ValidationError;
expect(error).toBeInstanceOf(ValidationError);
expect(error.cause).toEqual([
{
instancePath: "",
schemaPath: "#/definitions/SimpleType/type",
keyword: "type",
params: { type: "object" },
message: "must be object",
},
]);
}
});
});

0 comments on commit 41ed7c6

Please sign in to comment.