Skip to main content
Version: Current (v7.0.0-beta.3)

getClosestMatchingOption returns a valid option index whenever there are options

Detect​

  • In your code: calls to getClosestMatchingOption() or schemaUtils.getClosestMatchingOption() whose result is compared with -1, or that pass a selectedOption past the end of the options.
  • Tests that assert the index it returns for data that matches several options or none.

Change​

getClosestMatchingOption() returns an option the data matches whenever one does, and some option whenever there are options. A tie among options the data matches goes to the one holding each key its scope requires and taking the most keys the data holds, named under its properties or allowed as extra keys. A tied selectedOption written in the list keeps the tie. One a merge of lists built, as a then list merged with the field's own builds its pairs or a customMergeAllOf merges two or more lists an allOf brings together, keeps it only where it ranks as well. Where no option matches the data, a tie goes to the selectedOption when there is one, and to the first candidate otherwise. A selectedOption past the options is ignored. A property holding an anyOf/oneOf of its own adds the score of the option picked among those, plus one where the data matches it, where it used to add that option's index.

const schemaUtils = createSchemaUtils(context, rootSchema);
schemaUtils.getClosestMatchingOption(undefined, [{ type: 'string' }, { type: 'number' }]);
// Before: -1. After: 0
schemaUtils.getClosestMatchingOption(undefined, [{ type: 'string' }, { type: 'number' }], 2);
// Before: 2, an index past the options. After: 0

Drop any branch for -1 when the options aren't empty, and update tests that assert the old index.

Within one call, a pick is reused without looking at the data again, so a validator's isValid() must not change the data; see A custom validator's isValid must leave the data unchanged.

Verify​

Also read: