brutusin / json-forms

JSON Schema to HTML form generator, supporting dynamic subschemas (on the fly resolution). Extensible and customizable library with zero dependencies. Bootstrap add-ons provided
http://brutusin.org/json-forms
Apache License 2.0
607 stars 168 forks source link

Cannot validate fields of an object property #78

Closed joguiMultimedia closed 7 years ago

joguiMultimedia commented 7 years ago

Hi, and first thank you for your great work.

I am using a simple schema with properties referencing other properties defined in a separate schema through a $ref like this:

{
  "$schema": "http://json-schema.org/draft-03/schema#",
  "type": "object",
  "definitions": {
  "adress": {
  "$schema": "http://json-schema.org/draft-03/schema#",
  "type": "object",
  "required": true,
  "properties":{
    "NUMRUE": {
      "type": "string",
      "title": "Numero"
    },
    "NOMRUE": {
      "type": "string",
      "title": "Rue",
      "Required": true
    },
    "COMPLRUE": {
      "type": "string",
      "title": "Complément"
    },
    "CDP":{
      "type": "string",
      "title": "Code postal",
      "description": "12345",
      "pattern": "^\\d{5}$"
    },
    "VILLE":{
      "type": "string",
      "title": "Ville"
    },
    "PAYS":{
      "title": "Pays",
      "$ref": "#/definitions/adress/definitions/country",
      "required": true
    }
  }
  }
  },
  "properties": {
    "ADRESSE_CLI":{
      "type": "object",
      "title": "Adresse",
      "$ref": "#/definitions/adress"
    }
}
}

The issue is when validating the generated form the individual nested "adress" properties are not validated even though they are marked as "required" and leaved empty. Did I miss something?

Notes:

idelvall commented 7 years ago

Hi! You wrote

"NOMRUE": { "type": "string", "title": "Rue", "Required": true }

but "required" has to be in lowercase. This should fix your problem.... Cheers!

joguiMultimedia commented 7 years ago

As simple as that! Thanks it works. But, what about the $ref example? UPDATE: No validation error is thrown when none of the fields are filled but one is thrown when only one of them is filled (s1 OR s2). Is this the intent?

idelvall commented 7 years ago

That is a good question @joguiMultimedia!

Yes, it is the expected behavior. Nested required properties trigger a validation error when:

  1. The whole object is also required and the property is empty
  2. The whole object is not required, but it is being instantiated (other properties are informed)

It might be a little confusing at first, but it has sense. Think about an object that is not required, but some of its properties are. It is legal not informing it, but if informed the property has to be informed too.

Cheers

joguiMultimedia commented 7 years ago

It does make sense indeed. The confusion in my mind was that the object does not really exist (in terms of instanciation) even though the form's fields are displayed. Thanks for fast and accurate reply!