RJSF utility functions, constants and types
In version 5, the utility functions from @rjsf/core/utils were refactored into their own library called @rjsf/utils.
These utility functions are separated into two distinct groups.
The first, larger, group are the functions that do NOT require a ValidatorType interface be provided as one of their parameters.
The second, smaller, group are the functions that DO require a ValidatorType interface be provided as a parameter.
There is also a helper function used to create a SchemaUtilsType implementation from a ValidatorType implementation and rootSchema object.
Constants
The @rjsf/utils package exports a set of constants that represent all the keys into various elements of a RJSFSchema or UiSchema that are used by the various utility functions.
In addition to those keys, there is the special ADDITIONAL_PROPERTY_FLAG flag that is added to a schema under certain conditions by the retrieveSchema() utility.
These constants can be found on GitHub here.
Types
Additionally, the Typescript types used by the utility functions represent nearly all the types used by RJSF.
Those types are exported for use by @rjsf/core and all the themes, as well as any customizations you may build.
These types can be found on GitHub here.
Enums
There are enumerations in @rjsf/utils that are exported for use by @rjsf/core and all the themes, as well as any customizations you may build.
These enums can be found on GitHub here.
Non-Validator utility functions
allowAdditionalItems()
Checks the schema to see if it is allowing additional items, by verifying that schema.additionalItems is an object.
The user is warned in the console if schema.additionalItems has the value true.
Parameters
- schema: S - The schema object to check
Returns
- boolean: True if additional items is allowed, otherwise false
ariaDescribedByIds()
Return a list of element ids that contain additional information about the field that can be used to as the aria description of the field.
Parameters
- id: FieldPathId | string - Either simple string id or an FieldPathId from which to extract it
- [includeExamples=false]: boolean - Optional flag, if true, will add the
examplesIdinto the list
Returns
- string: The string containing the list of ids for use in an
aria-describedByattribute
asNumber()
Attempts to convert the string into a number. If an empty string is provided, then undefined is returned.
If a null is provided, it is returned.
If the string ends in a . then the string is returned because the user may be in the middle of typing a float number.
If a number ends in a pattern like .0, .20, .030, string is returned because the user may be typing number that will end in a non-zero digit.
Otherwise, the string is wrapped by Number() and if that result is not NaN, that number will be returned, otherwise the string value will be.
Parameters
- value: string | null - The string or null value to convert to a number
Returns
- undefined | null | string | number: The
valueconverted to a number when appropriate, otherwise thevalue
buttonId()
Return a consistent id for the btn button element
Parameters
- id: FieldPathId | string - The id of the parent component for the option
- btn: 'add' | 'copy' | 'moveDown' | 'moveUp' | 'remove' - The button type for which to generate the id
Returns
- string: The consistent id for the button from the given
idandbtntype
canExpand<T = any, S extends StrictRJSFSchema = RJSFSchema, F extends FormContextType = any>()
Checks whether the field described by schema, having the uiSchema and formData supports expanding.
The UI for the field can expand if it has additional properties, is not forced as non-expandable by the uiSchema and the formData object doesn't already have schema.maxProperties elements.
Parameters
- schema: S - The schema for the field that is being checked
- [uiSchema={}]: UiSchema<T, S, F> - The uiSchema for the field
- [formData]: T | undefined - The formData for the field
Returns
- boolean: True if the schema element has additionalProperties or patternProperties keywords, is expandable, and not at the maxProperties limit
createErrorHandler<T = any>()
Given a formData object, recursively creates a FormValidation error handling structure around it
Parameters
- formData: T - The form data around which the error handler is created
Returns
- FormValidation<T>: A
FormValidationobject based on theformDatastructure