forked from ajv-validator/ajv-cli
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add workaround for wrong schemaPath in ajv errors
- Loading branch information
Showing
6 changed files
with
82 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import traverse from '@json-schema-tools/traverse' | ||
import type { ErrorObject } from 'ajv' | ||
import type { JSONSchema7 as JSONSchema } from 'json-schema' | ||
|
||
import { deepClone } from './utils.js' | ||
|
||
// This is a workaround for https://github.com/ajv-validator/ajv/issues/512 | ||
|
||
const SchemaPathSymbol = Symbol('schemaPath') | ||
|
||
/** | ||
* Adds {@link SchemaPathSymbol} property with JSON Path to each schema object | ||
* in the given `jsonSchema`. This function **mutates** the given `jsonSchema`! | ||
*/ | ||
export function injectPathToSchemas(jsonSchema: JSONSchema, prefix?: string): void { | ||
prefix ??= `${jsonSchema.$id || ''}#` | ||
|
||
traverse.default( | ||
jsonSchema, | ||
(schema, _isCycle, path) => { | ||
if (!('$ref' in schema) && !(SchemaPathSymbol in schema)) { | ||
schema[SchemaPathSymbol] = `${prefix}${jsonPathToPointer(path)}` | ||
} | ||
return schema | ||
}, | ||
{ mutable: true }, | ||
) | ||
|
||
if (typeof jsonSchema.$defs === 'object') { | ||
for (const [key, schema] of Object.entries(jsonSchema.$defs)) { | ||
injectPathToSchemas(schema as JSONSchema, `${prefix}/$defs/${key}`) | ||
} | ||
} | ||
} | ||
|
||
/** @internal */ | ||
export function rewriteSchemaPathInErrors(errors: ErrorObject[], verbose: boolean): ErrorObject[] { | ||
return errors.map(error => { | ||
// The main purpose of this is to remove SchemaPathSymbol key. | ||
const copy = deepClone(error) | ||
if (error.parentSchema && SchemaPathSymbol in error.parentSchema) { | ||
copy.schemaPath = `${error.parentSchema[SchemaPathSymbol]}/${error.keyword}` | ||
if (!verbose) { | ||
delete copy.parentSchema | ||
delete copy.schema | ||
delete copy.data | ||
} | ||
} | ||
return copy | ||
}) | ||
} | ||
|
||
function jsonPathToPointer(jsonPath: string): string { | ||
if (jsonPath.startsWith('$')) { | ||
jsonPath = jsonPath.slice(1) | ||
} | ||
return jsonPath | ||
.split('.') | ||
.map(s => s.replaceAll(/\[(\d+)\]/g, '/$1')) | ||
.join('/') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters