React-jsonschema-form: Errors when parsing schema with a 'format' attribute

Created on 22 Jun 2018  路  4Comments  路  Source: rjsf-team/react-jsonschema-form

Prerequisites

  • [ ] I have read the documentation;
  • [ ] In the case of a bug report, I understand that providing a SSCCE example is tremendously useful to the maintainers.

Description

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?

Expected behavior

A successfully generated react form and no errors in the output

Actual behavior

The following error in the output in case properties in the schema have a format attribute.
Uncaught Error: No widget "double" for type "number"

question

Most helpful comment

@LucianBuzzo do you think rjsf should fail silently (or with a warning instead of an error) on unknown formats?

All 4 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sstarrAtmeta picture sstarrAtmeta  路  3Comments

elyobo picture elyobo  路  3Comments

abhishekpdubey picture abhishekpdubey  路  3Comments

j-zimnowoda picture j-zimnowoda  路  3Comments

anttivikman picture anttivikman  路  3Comments