@rjsf/utils
Version:
Utility functions for @rjsf/core
191 lines (190 loc) • 17.1 kB
TypeScript
import { Experimental_CustomMergeAllOf, FormContextType, RJSFSchema, StrictRJSFSchema, ValidatorType } from '../types.js';
/** Retrieves an expanded schema that has had all of its conditions, additional properties, references and dependencies
* resolved and merged into the `schema` given a `validator`, `rootSchema` and `rawFormData` that is used to do the
* potentially recursive resolution.
*
* @param validator - An implementation of the `ValidatorType` interface that will be forwarded to all the APIs
* @param schema - The schema for which retrieving a schema is desired
* @param [rootSchema={}] - The root schema that will be forwarded to all the APIs
* @param [rawFormData] - The current formData, if any, to assist retrieving a schema
* @param [experimental_customMergeAllOf] - Optional function that allows for custom merging of `allOf` schemas
* @returns - The schema having its conditions, additional properties, references and dependencies resolved
*/
export default function retrieveSchema<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(validator: ValidatorType<T, S, F>, schema: S, rootSchema?: S, rawFormData?: T, experimental_customMergeAllOf?: Experimental_CustomMergeAllOf<S>): S;
/** Resolves a conditional block (if/else/then) by removing the condition and merging the appropriate conditional branch
* with the rest of the schema. If `expandAllBranches` is true, then the `retrieveSchemaInteral()` results for both
* conditions will be returned.
*
* @param validator - An implementation of the `ValidatorType` interface that is used to detect valid schema conditions
* @param schema - The schema for which resolving a condition is desired
* @param rootSchema - The root schema that will be forwarded to all the APIs
* @param expandAllBranches - Flag, if true, will return all possible branches of conditions, any/oneOf and
* dependencies as a list of schemas
* @param recurseList - The list of recursive references already processed
* @param [formData] - The current formData to assist retrieving a schema
* @param [experimental_customMergeAllOf] - Optional function that allows for custom merging of `allOf` schemas
* @returns - A list of schemas with the appropriate conditions resolved, possibly with all branches expanded
*/
export declare function resolveCondition<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(validator: ValidatorType<T, S, F>, schema: S, rootSchema: S, expandAllBranches: boolean, recurseList: string[], formData?: T, experimental_customMergeAllOf?: Experimental_CustomMergeAllOf<S>): S[];
/** Given a list of lists of allOf, anyOf or oneOf values, create a list of lists of all permutations of the values. The
* `listOfLists` is expected to be all resolved values of the 1st...nth schemas within an `allOf`, `anyOf` or `oneOf`.
* From those lists, build a matrix for each `xxxOf` where there is more than one schema for a row in the list of lists.
*
* For example:
* - If there are three xxxOf rows (A, B, C) and they have been resolved such that there is only one A, two B and three
* C schemas then:
* - The permutation for the first row is `[[A]]`
* - The permutations for the second row are `[[A,B1], [A,B2]]`
* - The permutations for the third row are `[[A,B1,C1], [A,B1,C2], [A,B1,C3], [A,B2,C1], [A,B2,C2], [A,B2,C3]]`
*
* @param listOfLists - The list of lists of elements that represent the allOf, anyOf or oneOf resolved values in order
* @returns - The list of all permutations of schemas for a set of `xxxOf`s
*/
export declare function getAllPermutationsOfXxxOf<S extends StrictRJSFSchema = RJSFSchema>(listOfLists: S[][]): S[][];
/** Resolves references and dependencies within a schema and its 'allOf' children. Passes the `expandAllBranches` flag
* down to the `retrieveSchemaInternal()`, `resolveReference()` and `resolveDependencies()` helper calls. If
* `expandAllBranches` is true, then all possible dependencies and/or allOf branches are returned.
*
* @param validator - An implementation of the `ValidatorType` interface that will be forwarded to all the APIs
* @param schema - The schema for which resolving a schema is desired
* @param rootSchema - The root schema that will be forwarded to all the APIs
* @param expandAllBranches - Flag, if true, will return all possible branches of conditions, any/oneOf and dependencies
* as a list of schemas
* @param recurseList - The list of recursive references already processed
* @param [formData] - The current formData, if any, to assist retrieving a schema
* @param [experimental_customMergeAllOf] - Optional function that allows for custom merging of `allOf` schemas
* @returns - The list of schemas having its references, dependencies and allOf schemas resolved
*/
export declare function resolveSchema<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(validator: ValidatorType<T, S, F>, schema: S, rootSchema: S, expandAllBranches: boolean, recurseList: string[], formData?: T, experimental_customMergeAllOf?: Experimental_CustomMergeAllOf<S>): S[];
/** Resolves all references within a schema and then returns the `retrieveSchemaInternal()` if the resolved schema is
* actually different than the original. Passes the `expandAllBranches` flag down to the `retrieveSchemaInternal()`
* helper call.
*
* @param validator - An implementation of the `ValidatorType` interface that will be forwarded to all the APIs
* @param schema - The schema for which resolving a reference is desired
* @param rootSchema - The root schema that will be forwarded to all the APIs
* @param expandAllBranches - Flag, if true, will return all possible branches of conditions, any/oneOf and dependencies
* as a list of schemas
* @param recurseList - The list of recursive references already processed
* @param [formData] - The current formData, if any, to assist retrieving a schema
* @param [experimental_customMergeAllOf] - Optional function that allows for custom merging of `allOf` schemas
* @returns - The list schemas retrieved after having all references resolved
*/
export declare function resolveReference<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(validator: ValidatorType<T, S, F>, schema: S, rootSchema: S, expandAllBranches: boolean, recurseList: string[], formData?: T, experimental_customMergeAllOf?: Experimental_CustomMergeAllOf<S>): S[];
/** Resolves all references within the schema itself as well as any of its properties and array items.
*
* @param schema - The schema for which resolving all references is desired
* @param rootSchema - The root schema that will be forwarded to all the APIs
* @param recurseList - List of $refs already resolved to prevent recursion
* @returns - given schema will all references resolved or the original schema if no internal `$refs` were resolved
*/
export declare function resolveAllReferences<S extends StrictRJSFSchema = RJSFSchema>(schema: S, rootSchema: S, recurseList: string[]): S;
/** Creates new 'properties' items for each key in the `formData`
*
* @param validator - An implementation of the `ValidatorType` interface that will be used when necessary
* @param theSchema - The schema for which the existing additional properties is desired
* @param [rootSchema] - The root schema, used to primarily to look up `$ref`s * @param validator
* @param [aFormData] - The current formData, if any, to assist retrieving a schema
* @param [experimental_customMergeAllOf] - Optional function that allows for custom merging of `allOf` schemas
* @returns - The updated schema with additional properties stubbed
*/
export declare function stubExistingAdditionalProperties<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(validator: ValidatorType<T, S, F>, theSchema: S, rootSchema?: S, aFormData?: T, experimental_customMergeAllOf?: Experimental_CustomMergeAllOf<S>): S;
/** Internal handler that retrieves an expanded schema that has had all of its conditions, additional properties,
* references and dependencies resolved and merged into the `schema` given a `validator`, `rootSchema` and `rawFormData`
* that is used to do the potentially recursive resolution. If `expandAllBranches` is true, then all possible branches
* of the schema and its references, conditions and dependencies are returned.
*
* @param validator - An implementation of the `ValidatorType` interface that will be forwarded to all the APIs
* @param schema - The schema for which retrieving a schema is desired
* @param rootSchema - The root schema that will be forwarded to all the APIs
* @param [rawFormData] - The current formData, if any, to assist retrieving a schema
* @param [expandAllBranches=false] - Flag, if true, will return all possible branches of conditions, any/oneOf and
* dependencies as a list of schemas
* @param [recurseList=[]] - The optional, list of recursive references already processed
* @param [experimental_customMergeAllOf] - Optional function that allows for custom merging of `allOf` schemas
* @returns - The schema(s) resulting from having its conditions, additional properties, references and dependencies
* resolved. Multiple schemas may be returned if `expandAllBranches` is true.
*/
export declare function retrieveSchemaInternal<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(validator: ValidatorType<T, S, F>, schema: S, rootSchema: S, rawFormData?: T, expandAllBranches?: boolean, recurseList?: string[], experimental_customMergeAllOf?: Experimental_CustomMergeAllOf<S>): S[];
/** Resolves an `anyOf` or `oneOf` within a schema (if present) to the list of schemas returned from
* `retrieveSchemaInternal()` for the best matching option. If `expandAllBranches` is true, then a list of schemas for ALL
* options are retrieved and returned.
*
* @param validator - An implementation of the `ValidatorType` interface that will be forwarded to all the APIs
* @param schema - The schema for which retrieving a schema is desired
* @param rootSchema - The root schema that will be forwarded to all the APIs
* @param expandAllBranches - Flag, if true, will return all possible branches of conditions, any/oneOf and dependencies
* as a list of schemas
* @param [rawFormData] - The current formData, if any, to assist retrieving a schema, defaults to an empty object
* @returns - Either an array containing the best matching option or all options if `expandAllBranches` is true
*/
export declare function resolveAnyOrOneOfSchemas<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(validator: ValidatorType<T, S, F>, schema: S, rootSchema: S, expandAllBranches: boolean, rawFormData?: T): S[];
/** Resolves dependencies within a schema and its 'anyOf/oneOf' children. Passes the `expandAllBranches` flag down to
* the `resolveAnyOrOneOfSchema()` and `processDependencies()` helper calls.
*
* @param validator - An implementation of the `ValidatorType` interface that will be forwarded to all the APIs
* @param schema - The schema for which resolving a dependency is desired
* @param rootSchema - The root schema that will be forwarded to all the APIs
* @param expandAllBranches - Flag, if true, will return all possible branches of conditions, any/oneOf and dependencies
* as a list of schemas
* @param recurseList - The list of recursive references already processed
* @param [formData] - The current formData, if any, to assist retrieving a schema
* @param [experimental_customMergeAllOf] - Optional function that allows for custom merging of `allOf` schemas
* @returns - The list of schemas with their dependencies resolved
*/
export declare function resolveDependencies<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(validator: ValidatorType<T, S, F>, schema: S, rootSchema: S, expandAllBranches: boolean, recurseList: string[], formData?: T, experimental_customMergeAllOf?: Experimental_CustomMergeAllOf<S>): S[];
/** Processes all the `dependencies` recursively into the list of `resolvedSchema`s as needed. Passes the
* `expandAllBranches` flag down to the `withDependentSchema()` and the recursive `processDependencies()` helper calls.
*
* @param validator - An implementation of the `ValidatorType` interface that will be forwarded to all the APIs
* @param dependencies - The set of dependencies that needs to be processed
* @param resolvedSchema - The schema for which processing dependencies is desired
* @param rootSchema - The root schema that will be forwarded to all the APIs
* @param expandAllBranches - Flag, if true, will return all possible branches of conditions, any/oneOf and dependencies
* as a list of schemas
* @param recurseList - The list of recursive references already processed
* @param [formData] - The current formData, if any, to assist retrieving a schema
* @param [experimental_customMergeAllOf] - Optional function that allows for custom merging of `allOf` schemas
* @returns - The schema with the `dependencies` resolved into it
*/
export declare function processDependencies<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(validator: ValidatorType<T, S, F>, dependencies: S['dependencies'], resolvedSchema: S, rootSchema: S, expandAllBranches: boolean, recurseList: string[], formData?: T, experimental_customMergeAllOf?: Experimental_CustomMergeAllOf<S>): S[];
/** Updates a schema with additionally required properties added
*
* @param schema - The schema for which resolving a dependent properties is desired
* @param [additionallyRequired] - An optional array of additionally required names
* @returns - The schema with the additional required values merged in
*/
export declare function withDependentProperties<S extends StrictRJSFSchema = RJSFSchema>(schema: S, additionallyRequired?: string[]): S;
/** Merges a dependent schema into the `schema` dealing with oneOfs and references. Passes the `expandAllBranches` flag
* down to the `retrieveSchemaInternal()`, `resolveReference()` and `withExactlyOneSubschema()` helper calls.
*
* @param validator - An implementation of the `ValidatorType` interface that will be forwarded to all the APIs
* @param schema - The schema for which resolving a dependent schema is desired
* @param rootSchema - The root schema that will be forwarded to all the APIs
* @param dependencyKey - The key name of the dependency
* @param dependencyValue - The potentially dependent schema
* @param expandAllBranches - Flag, if true, will return all possible branches of conditions, any/oneOf and dependencies
* as a list of schemas
* @param recurseList - The list of recursive references already processed
* @param [formData]- The current formData to assist retrieving a schema
* @param [experimental_customMergeAllOf] - Optional function that allows for custom merging of `allOf` schemas
* @returns - The list of schemas with the dependent schema resolved into them
*/
export declare function withDependentSchema<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(validator: ValidatorType<T, S, F>, schema: S, rootSchema: S, dependencyKey: string, dependencyValue: S, expandAllBranches: boolean, recurseList: string[], formData?: T, experimental_customMergeAllOf?: Experimental_CustomMergeAllOf<S>): S[];
/** Returns a list of `schema`s with the best choice from the `oneOf` options merged into it. If `expandAllBranches` is
* true, then a list of schemas for ALL options are retrieved and returned. Passes the `expandAllBranches` flag down to
* the `retrieveSchemaInternal()` helper call.
*
* @param validator - An implementation of the `ValidatorType` interface that will be used to validate oneOf options
* @param schema - The schema for which resolving a oneOf subschema is desired
* @param rootSchema - The root schema that will be forwarded to all the APIs
* @param dependencyKey - The key name of the oneOf dependency
* @param oneOf - The list of schemas representing the oneOf options
* @param expandAllBranches - Flag, if true, will return all possible branches of conditions, any/oneOf and dependencies
* as a list of schemas
* @param recurseList - The list of recursive references already processed
* @param [formData] - The current formData to assist retrieving a schema
* @param [experimental_customMergeAllOf] - Optional function that allows for custom merging of `allOf` schemas
* @returns - Either an array containing the best matching option or all options if `expandAllBranches` is true
*/
export declare function withExactlyOneSubschema<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>(validator: ValidatorType<T, S, F>, schema: S, rootSchema: S, dependencyKey: string, oneOf: S['oneOf'], expandAllBranches: boolean, recurseList: string[], formData?: T, experimental_customMergeAllOf?: Experimental_CustomMergeAllOf<S>): S[];