I'm facing a problem generating a form from a JSON schema.
The API that I use to generate a JSON schema from C# types adds a format attribute to the properties.
Example:
{
"title": "SomeClass",
"type": "object",
"additionalProperties": false,
"properties": {
"Number": {
"type": "number",
"format": "double"
}
}
}
Apparently "format": "double" doesn't allow to render a form and I'm curious if there is any way I can avoid it or let react-jsonschema ignore the attribute when parsing?
A successfully generated react form and no errors in the output
The following error in the output in case properties in the schema have a format attribute.
Uncaught Error: No widget "double" for type "number"
I ran into the same issue and implemented a utility function that would remove unknown schema formats.
const SUPPORTED_SCHEMA_FORMATS = [
'data-url',
'date',
'date-time',
'email',
'hostname',
'ipv4',
'ipv6',
'uri',
];
const stripSchemaFormats = (
schema,
whitelist
) => {
const newSchema = cloneDeep(schema);
const _strip = (schema) => {
if (schema.format && whitelist.indexOf(schema.format) === -1) {
delete schema.format;
}
if (schema.properties) {
forEach(schema.properties, subSchema => {
_strip(subSchema);
});
}
};
_strip(newSchema);
return newSchema;
};
const schema = {
title: 'SomeClass',
type: 'object',
additionalProperties: false,
properties: {
Number: {
type: 'number',
format: 'double'
}
}
}
const parsedSchema = stripSchemaFormats(schema, SUPPORTED_SCHEMA_FORMATS);
@LucianBuzzo do you think rjsf should fail silently (or with a warning instead of an error) on unknown formats?
@LucianBuzzo do you think rjsf should fail silently (or with a warning instead of an error) on unknown formats?
Yes. Yes it should. From the spec (emphasis mine):
The value of this keyword is called a format attribute. It MUST be a string. A format attribute can generally only validate a given set of instance types. If the type of the instance to validate is not in this set, validation for this format attribute and instance SHOULD succeed.
Forgot to add a mention for @epicfaace.
Most helpful comment
@LucianBuzzo do you think rjsf should fail silently (or with a warning instead of an error) on unknown formats?