@@ -10,7 +10,7 @@ import getFirstMatchingOption from './getFirstMatchingOption';
1010import retrieveSchema , { resolveAllReferences } from './retrieveSchema' ;
1111import { ONE_OF_KEY , REF_KEY , JUNK_OPTION_ID , ANY_OF_KEY } from '../constants' ;
1212import guessType from '../guessType' ;
13- import { FormContextType , RJSFSchema , StrictRJSFSchema , ValidatorType } from '../types' ;
13+ import { Experimental_CustomMergeAllOf , FormContextType , RJSFSchema , StrictRJSFSchema , ValidatorType } from '../types' ;
1414import getDiscriminatorFieldFromSchema from '../getDiscriminatorFieldFromSchema' ;
1515import getOptionMatchingSimpleDiscriminator from '../getOptionMatchingSimpleDiscriminator' ;
1616
@@ -45,13 +45,15 @@ export const JUNK_OPTION: StrictRJSFSchema = {
4545 * @param rootSchema - The root JSON schema of the entire form
4646 * @param schema - The schema for which the score is being calculated
4747 * @param formData - The form data associated with the schema, used to calculate the score
48+ * @param [experimental_customMergeAllOf] - Optional function that allows for custom merging of `allOf` schemas
4849 * @returns - The score a schema against the formData
4950 */
5051export function calculateIndexScore < T = any , S extends StrictRJSFSchema = RJSFSchema , F extends FormContextType = any > (
5152 validator : ValidatorType < T , S , F > ,
5253 rootSchema : S ,
5354 schema ?: S ,
54- formData ?: any
55+ formData ?: any ,
56+ experimental_customMergeAllOf ?: Experimental_CustomMergeAllOf < S >
5557) : number {
5658 let totalScore = 0 ;
5759 if ( schema ) {
@@ -64,8 +66,23 @@ export function calculateIndexScore<T = any, S extends StrictRJSFSchema = RJSFSc
6466 return score ;
6567 }
6668 if ( has ( value , REF_KEY ) ) {
67- const newSchema = retrieveSchema < T , S , F > ( validator , value as S , rootSchema , formValue ) ;
68- return score + calculateIndexScore < T , S , F > ( validator , rootSchema , newSchema , formValue || { } ) ;
69+ const newSchema = retrieveSchema < T , S , F > (
70+ validator ,
71+ value as S ,
72+ rootSchema ,
73+ formValue ,
74+ experimental_customMergeAllOf
75+ ) ;
76+ return (
77+ score +
78+ calculateIndexScore < T , S , F > (
79+ validator ,
80+ rootSchema ,
81+ newSchema ,
82+ formValue || { } ,
83+ experimental_customMergeAllOf
84+ )
85+ ) ;
6986 }
7087 if ( ( has ( value , ONE_OF_KEY ) || has ( value , ANY_OF_KEY ) ) && formValue ) {
7188 const key = has ( value , ONE_OF_KEY ) ? ONE_OF_KEY : ANY_OF_KEY ;
@@ -78,7 +95,8 @@ export function calculateIndexScore<T = any, S extends StrictRJSFSchema = RJSFSc
7895 formValue ,
7996 get ( value , key ) as S [ ] ,
8097 - 1 ,
81- discriminator
98+ discriminator ,
99+ experimental_customMergeAllOf
82100 )
83101 ) ;
84102 }
@@ -87,7 +105,10 @@ export function calculateIndexScore<T = any, S extends StrictRJSFSchema = RJSFSc
87105 // If the structure is matching then give it a little boost in score
88106 score += 1 ;
89107 }
90- return score + calculateIndexScore < T , S , F > ( validator , rootSchema , value as S , formValue ) ;
108+ return (
109+ score +
110+ calculateIndexScore < T , S , F > ( validator , rootSchema , value as S , formValue , experimental_customMergeAllOf )
111+ ) ;
91112 }
92113 if ( value . type === guessType ( formValue ) ) {
93114 // If the types match, then we bump the score by one
@@ -135,6 +156,7 @@ export function calculateIndexScore<T = any, S extends StrictRJSFSchema = RJSFSc
135156 * @param [selectedOption=-1] - The index of the currently selected option, defaulted to -1 if not specified
136157 * @param [discriminatorField] - The optional name of the field within the options object whose value is used to
137158 * determine which option is selected
159+ * @param [experimental_customMergeAllOf] - Optional function that allows for custom merging of `allOf` schemas
138160 * @returns - The index of the option that is the closest match to the `formData` or the `selectedOption` if no match
139161 */
140162export default function getClosestMatchingOption <
@@ -147,7 +169,8 @@ export default function getClosestMatchingOption<
147169 formData : T | undefined ,
148170 options : S [ ] ,
149171 selectedOption = - 1 ,
150- discriminatorField ?: string
172+ discriminatorField ?: string ,
173+ experimental_customMergeAllOf ?: Experimental_CustomMergeAllOf < S >
151174) : number {
152175 // First resolve any refs in the options
153176 const resolvedOptions = options . map ( ( option ) => {
@@ -185,7 +208,7 @@ export default function getClosestMatchingOption<
185208 ( scoreData : BestType , index : number ) => {
186209 const { bestScore } = scoreData ;
187210 const option = resolvedOptions [ index ] ;
188- const score = calculateIndexScore ( validator , rootSchema , option , formData ) ;
211+ const score = calculateIndexScore ( validator , rootSchema , option , formData , experimental_customMergeAllOf ) ;
189212 scoreCount . add ( score ) ;
190213 if ( score > bestScore ) {
191214 return { bestIndex : index , bestScore : score } ;
0 commit comments