Skip to content

Commit 16a7eb8

Browse files
committed
Add more data to SocketValidationError
To make it compatible with eg. @apideck/better-ajv-errors
1 parent 7a54aa8 commit 16a7eb8

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,13 @@ A JSON Schema object typed with [`JSONSchemaType<SocketYml>`](https://ajv.js.org
4343

4444
### `SocketValidationError`
4545

46-
Error thrown when the parsed data doesn't conform to the JSON Schema definition (see `socketYmlSchema`).
46+
Error thrown when the parsed data doesn't conform to the JSON Schema definition.
4747

48-
Exposes an array of [Ajv's `ErrorObject`](https://ajv.js.org/api.html#error-objects) at `.validationErrors`, allowing a consumer to present a user friendly error.
48+
Extends `Error` and adds these additional properties:
49+
50+
* `data` – the data that's found to be invalid
51+
* `schema` – the schema used to validate the content
52+
* `validationErrors` – an array of [Ajv's `ErrorObject`](https://ajv.js.org/api.html#error-objects)
4953

5054
## Type exports
5155

index.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,11 @@ async function parseSocketConfig (fileContent) {
102102
}
103103

104104
if (!validate(parsedContent)) {
105-
throw new SocketValidationError('Invalid config definition', validate.errors || [])
105+
throw new SocketValidationError(
106+
'Invalid config definition',
107+
validate.errors || [],
108+
parsedContent
109+
)
106110
}
107111

108112
return parsedContent
@@ -126,10 +130,17 @@ class SocketValidationError extends Error {
126130
/**
127131
* @param {string} message
128132
* @param {import('ajv').ErrorObject[]} validationErrors
133+
* @param {unknown} parsedContent
129134
*/
130-
constructor (message, validationErrors) {
135+
constructor (message, validationErrors, parsedContent) {
131136
super(message)
132137

138+
/** @type {unknown} */
139+
this.data = parsedContent
140+
141+
/** @type {import('ajv').JSONSchemaType<SocketYml>} */
142+
this.schema = socketYmlSchema
143+
133144
/** @type {import('ajv').ErrorObject[]} */
134145
this.validationErrors = validationErrors
135146
}

0 commit comments

Comments
 (0)