React-jsonschema-form: Validation on Null value "Is not of a type(s) string"

Created on 12 Mar 2017  路  13Comments  路  Source: rjsf-team/react-jsonschema-form

Description

Validation on type face problem when binding data from DB null.
The submit /post will not fire due to validation failed but if user simple enter some text and then erase it in the text box, the validation passed.

Way to produce

  1. Binding Null data in to text box

1

  1. Click on submit, error message will show.

2

  1. Type any text in text box and erase in by "backspace".
  2. Click on Submit. Post fired.

Most helpful comment

I think there should be some supported solution for this, because null is a valid value if the field is not marked as required.

All 13 comments

Indeed null is a specific jsonschema type and not a string. Try casting your null into undefined to mark the field unfilled.

@n1k0 null is jasonschma type?

I try set "type": "null" as below but i got "Unsupported field schema { "type": "null"..."

"ID_NUMBER": {
"type": "null",
"title": "IC Number",
"pattern": "^$|^((\d{2}((0[13578]|1[02])(0[1-9]|[12]\d|3[01])|(0[13456789]|1[012])(0[1-9]|[12]\d|30)|02(0[1-9]|1\d|2[0-8])))|([02468][048]|[13579][26])0229)\d{2}\d{4}$",
"messages": {
pattern: "Invalid ic number format.",
}

The null jsonschema type only accepts null values (and supporting a null field wouldn't make any sense in a HTML forms library). You seem to be after a nullable string type, eg. ["string", "null"] (where a provided value could either be a string or null), but this isn't supported in this lib (see #282).

Still, turning your null DB values into js undefined ones should solve your initial use case here.

Thoughts on a form option to cast nulls to undefined? For now I filter using the transformError prop - errors.filter(error => !!error.instance)

I think there should be some supported solution for this, because null is a valid value if the field is not marked as required.

null is a valid value if the field is not marked as required.

If a field is not required, the object property should either be missing or set to the expected type:

So, I'm doing a PATCH operation from all the field values. If I set nulls as undefined, I'm bypassing the validation but... I'm losing the fields for patching and the server thinks I'm just not sending any value for modification.

This is a client side form library. What you send to your server should probably be post-processed. Also, if your server expects null or string values for example, you should type your schema accordingly, with a composite schema type like ["null", "string"], though that is not supported by this lib yet.

But maybe someone wants to work on a patch?

Derived from @1dolinski's comment, I'm currently filtering errors like this:

transformErrors={errors => errors.filter(error => error.hasOwnProperty('instance'))}

If I have some time left towards the end of the week, I'll look into creating a PR to support ["null", "string"].

What was the outcome of this thread? Is the expectation to use

transformErrors={errors => errors.filter(error => error.hasOwnProperty('instance'))}

or is there support for ["null","string"] types?

+1 for support for ["null","string"]

+1 for support for ["null","string"]

["null", "string"] seems like a duplicate of #465.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mfulton26 picture mfulton26  路  3Comments

sstarrAtmeta picture sstarrAtmeta  路  3Comments

n1k0 picture n1k0  路  3Comments

jabaren picture jabaren  路  3Comments

anttivikman picture anttivikman  路  3Comments