@rjsf/utils
Advanced tools
Comparing version 5.18.1 to 5.18.2
@@ -11,5 +11,18 @@ import { GenericObjectType, RJSFSchema, StrictRJSFSchema } from './types'; | ||
export declare function splitKeyElementFromObject(key: string, object: GenericObjectType): any[]; | ||
/** Given the name of a `$ref` from within a schema, using the `rootSchema`, recursively look up and return the | ||
* sub-schema using the path provided by that reference. If `#` is not the first character of the reference, the path | ||
* does not exist in the schema, or the reference resolves circularly back to itself, then throw an Error. | ||
* Otherwise return the sub-schema. Also deals with nested `$ref`s in the sub-schema. | ||
* | ||
* @param $ref - The ref string for which the schema definition is desired | ||
* @param [rootSchema={}] - The root schema in which to search for the definition | ||
* @param recurseList - List of $refs already resolved to prevent recursion | ||
* @returns - The sub-schema within the `rootSchema` which matches the `$ref` if it exists | ||
* @throws - Error indicating that no schema for that reference could be resolved | ||
*/ | ||
export declare function findSchemaDefinitionRecursive<S extends StrictRJSFSchema = RJSFSchema>($ref?: string, rootSchema?: S, recurseList?: string[]): S; | ||
/** Given the name of a `$ref` from within a schema, using the `rootSchema`, look up and return the sub-schema using the | ||
* path provided by that reference. If `#` is not the first character of the reference, or the path does not exist in | ||
* the schema, then throw an Error. Otherwise return the sub-schema. Also deals with nested `$ref`s in the sub-schema. | ||
* path provided by that reference. If `#` is not the first character of the reference, the path does not exist in | ||
* the schema, or the reference resolves circularly back to itself, then throw an Error. Otherwise return the | ||
* sub-schema. Also deals with nested `$ref`s in the sub-schema. | ||
* | ||
@@ -19,4 +32,4 @@ * @param $ref - The ref string for which the schema definition is desired | ||
* @returns - The sub-schema within the `rootSchema` which matches the `$ref` if it exists | ||
* @throws - Error indicating that no schema for that reference exists | ||
* @throws - Error indicating that no schema for that reference could be resolved | ||
*/ | ||
export default function findSchemaDefinition<S extends StrictRJSFSchema = RJSFSchema>($ref?: string, rootSchema?: S): S; |
@@ -17,16 +17,19 @@ import jsonpointer from 'jsonpointer'; | ||
} | ||
/** Given the name of a `$ref` from within a schema, using the `rootSchema`, look up and return the sub-schema using the | ||
* path provided by that reference. If `#` is not the first character of the reference, or the path does not exist in | ||
* the schema, then throw an Error. Otherwise return the sub-schema. Also deals with nested `$ref`s in the sub-schema. | ||
/** Given the name of a `$ref` from within a schema, using the `rootSchema`, recursively look up and return the | ||
* sub-schema using the path provided by that reference. If `#` is not the first character of the reference, the path | ||
* does not exist in the schema, or the reference resolves circularly back to itself, then throw an Error. | ||
* Otherwise return the sub-schema. Also deals with nested `$ref`s in the sub-schema. | ||
* | ||
* @param $ref - The ref string for which the schema definition is desired | ||
* @param [rootSchema={}] - The root schema in which to search for the definition | ||
* @param recurseList - List of $refs already resolved to prevent recursion | ||
* @returns - The sub-schema within the `rootSchema` which matches the `$ref` if it exists | ||
* @throws - Error indicating that no schema for that reference exists | ||
* @throws - Error indicating that no schema for that reference could be resolved | ||
*/ | ||
export default function findSchemaDefinition($ref, rootSchema = {}) { | ||
let ref = $ref || ''; | ||
export function findSchemaDefinitionRecursive($ref, rootSchema = {}, recurseList = []) { | ||
const ref = $ref || ''; | ||
let decodedRef; | ||
if (ref.startsWith('#')) { | ||
// Decode URI fragment representation. | ||
ref = decodeURIComponent(ref.substring(1)); | ||
decodedRef = decodeURIComponent(ref.substring(1)); | ||
} | ||
@@ -36,9 +39,19 @@ else { | ||
} | ||
const current = jsonpointer.get(rootSchema, ref); | ||
const current = jsonpointer.get(rootSchema, decodedRef); | ||
if (current === undefined) { | ||
throw new Error(`Could not find a definition for ${$ref}.`); | ||
} | ||
if (current[REF_KEY]) { | ||
const nextRef = current[REF_KEY]; | ||
if (nextRef) { | ||
// Check for circular references. | ||
if (recurseList.includes(nextRef)) { | ||
if (recurseList.length === 1) { | ||
throw new Error(`Definition for ${$ref} is a circular reference`); | ||
} | ||
const [firstRef, ...restRefs] = recurseList; | ||
const circularPath = [...restRefs, ref, firstRef].join(' -> '); | ||
throw new Error(`Definition for ${firstRef} contains a circular reference through ${circularPath}`); | ||
} | ||
const [remaining, theRef] = splitKeyElementFromObject(REF_KEY, current); | ||
const subSchema = findSchemaDefinition(theRef, rootSchema); | ||
const subSchema = findSchemaDefinitionRecursive(theRef, rootSchema, [...recurseList, ref]); | ||
if (Object.keys(remaining).length > 0) { | ||
@@ -51,2 +64,16 @@ return { ...remaining, ...subSchema }; | ||
} | ||
/** Given the name of a `$ref` from within a schema, using the `rootSchema`, look up and return the sub-schema using the | ||
* path provided by that reference. If `#` is not the first character of the reference, the path does not exist in | ||
* the schema, or the reference resolves circularly back to itself, then throw an Error. Otherwise return the | ||
* sub-schema. Also deals with nested `$ref`s in the sub-schema. | ||
* | ||
* @param $ref - The ref string for which the schema definition is desired | ||
* @param [rootSchema={}] - The root schema in which to search for the definition | ||
* @returns - The sub-schema within the `rootSchema` which matches the `$ref` if it exists | ||
* @throws - Error indicating that no schema for that reference could be resolved | ||
*/ | ||
export default function findSchemaDefinition($ref, rootSchema = {}) { | ||
const recurseList = []; | ||
return findSchemaDefinitionRecursive($ref, rootSchema, recurseList); | ||
} | ||
//# sourceMappingURL=findSchemaDefinition.js.map |
{ | ||
"name": "@rjsf/utils", | ||
"version": "5.18.1", | ||
"version": "5.18.2", | ||
"main": "dist/index.js", | ||
@@ -89,3 +89,3 @@ "module": "lib/index.js", | ||
"license": "Apache-2.0", | ||
"gitHead": "6b75c315b0d457848d06cb3e6389b1dd9c6ddb57" | ||
"gitHead": "31539d1cbe6e685e694bc7837381598460e00502" | ||
} |
@@ -21,29 +21,43 @@ import jsonpointer from 'jsonpointer'; | ||
/** Given the name of a `$ref` from within a schema, using the `rootSchema`, look up and return the sub-schema using the | ||
* path provided by that reference. If `#` is not the first character of the reference, or the path does not exist in | ||
* the schema, then throw an Error. Otherwise return the sub-schema. Also deals with nested `$ref`s in the sub-schema. | ||
/** Given the name of a `$ref` from within a schema, using the `rootSchema`, recursively look up and return the | ||
* sub-schema using the path provided by that reference. If `#` is not the first character of the reference, the path | ||
* does not exist in the schema, or the reference resolves circularly back to itself, then throw an Error. | ||
* Otherwise return the sub-schema. Also deals with nested `$ref`s in the sub-schema. | ||
* | ||
* @param $ref - The ref string for which the schema definition is desired | ||
* @param [rootSchema={}] - The root schema in which to search for the definition | ||
* @param recurseList - List of $refs already resolved to prevent recursion | ||
* @returns - The sub-schema within the `rootSchema` which matches the `$ref` if it exists | ||
* @throws - Error indicating that no schema for that reference exists | ||
* @throws - Error indicating that no schema for that reference could be resolved | ||
*/ | ||
export default function findSchemaDefinition<S extends StrictRJSFSchema = RJSFSchema>( | ||
export function findSchemaDefinitionRecursive<S extends StrictRJSFSchema = RJSFSchema>( | ||
$ref?: string, | ||
rootSchema: S = {} as S | ||
rootSchema: S = {} as S, | ||
recurseList: string[] = [] | ||
): S { | ||
let ref = $ref || ''; | ||
const ref = $ref || ''; | ||
let decodedRef; | ||
if (ref.startsWith('#')) { | ||
// Decode URI fragment representation. | ||
ref = decodeURIComponent(ref.substring(1)); | ||
decodedRef = decodeURIComponent(ref.substring(1)); | ||
} else { | ||
throw new Error(`Could not find a definition for ${$ref}.`); | ||
} | ||
const current: S = jsonpointer.get(rootSchema, ref); | ||
const current: S = jsonpointer.get(rootSchema, decodedRef); | ||
if (current === undefined) { | ||
throw new Error(`Could not find a definition for ${$ref}.`); | ||
} | ||
if (current[REF_KEY]) { | ||
const nextRef = current[REF_KEY]; | ||
if (nextRef) { | ||
// Check for circular references. | ||
if (recurseList.includes(nextRef)) { | ||
if (recurseList.length === 1) { | ||
throw new Error(`Definition for ${$ref} is a circular reference`); | ||
} | ||
const [firstRef, ...restRefs] = recurseList; | ||
const circularPath = [...restRefs, ref, firstRef].join(' -> '); | ||
throw new Error(`Definition for ${firstRef} contains a circular reference through ${circularPath}`); | ||
} | ||
const [remaining, theRef] = splitKeyElementFromObject(REF_KEY, current); | ||
const subSchema = findSchemaDefinition<S>(theRef, rootSchema); | ||
const subSchema = findSchemaDefinitionRecursive<S>(theRef, rootSchema, [...recurseList, ref]); | ||
if (Object.keys(remaining).length > 0) { | ||
@@ -56,1 +70,19 @@ return { ...remaining, ...subSchema }; | ||
} | ||
/** Given the name of a `$ref` from within a schema, using the `rootSchema`, look up and return the sub-schema using the | ||
* path provided by that reference. If `#` is not the first character of the reference, the path does not exist in | ||
* the schema, or the reference resolves circularly back to itself, then throw an Error. Otherwise return the | ||
* sub-schema. Also deals with nested `$ref`s in the sub-schema. | ||
* | ||
* @param $ref - The ref string for which the schema definition is desired | ||
* @param [rootSchema={}] - The root schema in which to search for the definition | ||
* @returns - The sub-schema within the `rootSchema` which matches the `$ref` if it exists | ||
* @throws - Error indicating that no schema for that reference could be resolved | ||
*/ | ||
export default function findSchemaDefinition<S extends StrictRJSFSchema = RJSFSchema>( | ||
$ref?: string, | ||
rootSchema: S = {} as S | ||
): S { | ||
const recurseList: string[] = []; | ||
return findSchemaDefinitionRecursive($ref, rootSchema, recurseList); | ||
} |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1624085
19469