ngx-schema-form icon indicating copy to clipboard operation
ngx-schema-form copied to clipboard

Validation of non required fields

Open daniele-pecora opened this issue 7 years ago • 4 comments

When validating a form, a non required empty field gets a validation error. The schema for that field that is passed to the validation function is reduced to the part of the corresponding property. There is no way the validator could know if this field is required or not. As far as I have seen in the code of the ZSchemaValidatorFactory the validation errors of required fields are set up by the surrounding parent in the function denormalizeRequiredPropertyPaths.

So when you have a field which is optional but has to have a minLength if given, this can't be validated right at the moment.

Regarding to this discussion at Z-Schema https://github.com/zaggino/z-schema/issues/49 the validation of non required empty fields can be skipped by extending the property type with "null" and then passing a null value instead of an empty string.

e.g.

  "name":{
        "type":["string", "null"]
   }

so this could be defined for all non required fields and let keep all minLength , pattern etc. validation rules.

daniele-pecora avatar Dec 10 '17 08:12 daniele-pecora

Hello

I ran into the same issue, IMHO an optional property should not be validated against any constraint, the problem should be fixed at the validator. That said, to workaround the ZSchema issue, we can avoid validation process on non required field that have no values. Then the question is what is an empty field in terms of Form.

Schema validation should be skipped in some condition in the following piece of code :

extract from 'fromproperty.ts'

public _runValidation(): any { let errors = this.schemaValidator(this._value) || []; let customValidator = this.validatorRegistry.get(this.path); if (customValidator) { let customErrors = customValidator(this.value, this, this.findRoot()); errors = this.mergeErrors(errors, customErrors); } if (errors.length === 0) { errors = null; } this._errors = errors; this.setErrors(this._errors); }

I guess I'll have to provide more information to the formproperty like a 'required' boolean to implement validation skip mechanism.

I'll give it a try.

ccordenier avatar Dec 13 '17 09:12 ccordenier

Just did a simple test

  • In form property factory, check if field is required by analyzing 'required' property of parent schema, and add required property in formproperty class constructor

let required = parent != null && parent.schema['required'] && parent.schema['required'].indexOf(propertyId) != -1;

  • In formproperty _runValidation, skip validation when formproperty is not required and has no value

// Skip validation for optional fields that have no value if (!this._required && !this._hasValue()) { return; }

And it seems to work as expected. Not need to add "null" type in schema.

Any opinion from makinacorpus team ?

ccordenier avatar Dec 13 '17 10:12 ccordenier

have example for "Validation handled by z-schema "? @daniele-pecora thanks.

JianhuisHuang avatar May 09 '19 06:05 JianhuisHuang

@JianhuisHuang We worked around this by using a custom validator factory, which does take in count if an optional field is empty. Here is how we use it: https://github.com/daniele-pecora/ngx-schema-form-widgets-material/blob/master/projects/ngx-schema-form-widgets-material/src/lib/fix-optional-empty-fields-z-schema-validator-factory.ts

daniele-pecora avatar Nov 17 '20 06:11 daniele-pecora