diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2023-12-19 00:33:38 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-19 05:33:38 +0000 |
commit | 1ced9aafc00c10992bab8bd3f1b6b1397f05b7b9 (patch) | |
tree | 305bb2b3bfc7fc3b051ee1cd3d1c35f442af0de4 /dev/schema-validate.js | |
parent | 5f96276fda93dcad39f2165fd3c8d890aa5f9be5 (diff) |
Make JSON.parse usage safer (#373)
* Make JSON.parse usage safer
* Fix any type
* Add readResponseJson
* Use readResponseJson
* Additional updates
* Rename files
* Add types
Diffstat (limited to 'dev/schema-validate.js')
-rw-r--r-- | dev/schema-validate.js | 6 |
1 files changed, 4 insertions, 2 deletions
diff --git a/dev/schema-validate.js b/dev/schema-validate.js index 81953f49..57faf96c 100644 --- a/dev/schema-validate.js +++ b/dev/schema-validate.js @@ -20,6 +20,7 @@ import Ajv from 'ajv'; import {readFileSync} from 'fs'; import {JsonSchema} from '../ext/js/data/json-schema.js'; import {DataError} from './data-error.js'; +import {parseJson} from './json.js'; class JsonSchemaAjv { /** @@ -32,7 +33,8 @@ class JsonSchemaAjv { allowUnionTypes: true }); const metaSchemaPath = require.resolve('ajv/dist/refs/json-schema-draft-07.json'); - const metaSchema = JSON.parse(readFileSync(metaSchemaPath, {encoding: 'utf8'})); + /** @type {import('ajv').AnySchemaObject} */ + const metaSchema = parseJson(readFileSync(metaSchemaPath, {encoding: 'utf8'})); ajv.addMetaSchema(metaSchema); /** @type {import('ajv').ValidateFunction} */ this._validate = ajv.compile(/** @type {import('ajv').Schema} */ (schema)); @@ -46,7 +48,7 @@ class JsonSchemaAjv { if (this._validate(data)) { return; } const {errors} = this._validate; const error = new DataError('Schema validation failed'); - error.data = JSON.parse(JSON.stringify(errors)); + error.data = parseJson(JSON.stringify(errors)); throw error; } } |