json-schema-form / angular-schema-form

Generate forms from a JSON schema, with AngularJS!
https://json-schema-form.github.io/angular-schema-form
MIT License
2.47k stars 653 forks source link

schema.required.indexOf is not a function #794

Closed karthiks3000 closed 7 years ago

karthiks3000 commented 7 years ago

Bug

I have a schema where the array properties are generated with a required attribute at the array level and at the individual item level. But when I try to generate the form I get an error. TypeError: schema.required.indexOf is not a function at Array.array (schema-form.js:1211)

Here is the schema that does not work { "Genre": { "type": [ "array", "null" ], "required": true, "items": { "type": [ "object", "null" ], "properties": { "GenreName": { "required": true, "type": [ "string", "null" ] } } } } }

The above schema works if I were to remove the required attribute at the top most level.

This one works- { "Genre": { "type": [ "array", "null" ], "items": { "type": [ "object", "null" ], "properties": { "GenreName": { "required": true, "type": [ "string", "null" ] } } } } }

Related issues

This is/maybe related to the following lines of code

var required = schema.required && schema.required.indexOf(options.path[options.path.length - 1]) !== -1;

I was able to resolve the issue by making the below change, but I'm not sure if it will lead to some other issues.

var required = schema.required; if (schema.required && schema.required.length > 0) { required = schema.required && schema.required.indexOf(options.path[options.path.length - 1]) !== -1; } @json-schema-form/angular-schema-form-lead

Anthropic commented 7 years ago

@karthiks3000 required is not a boolean value, it must be an array.

Please refer to the JSON Schema specification on the attribute.

From the spec (uppercase emphasis theirs): 5.4.3.1. Valid values The value of this keyword MUST be an array. This array MUST have at least one element. Elements of this array MUST be strings, and MUST be unique.

karthiks3000 commented 7 years ago

@Anthropic Looks like your right! But if that were the case why does the following code work? { "Genre": { "type": [ "array", "null" ], "items": { "type": [ "object", "null" ], "properties": { "GenreName": { "required": true, "type": [ "string", "null" ] } } } } }

Anthropic commented 7 years ago

@karthiks3000 thanks, I assume that is misbehaving due to support for the value within the form description as opposed to the schema, but I haven't tested that to be sure.