guillotinaweb / ngx-schema-form

HTML form generation based on JSON Schema
MIT License
483 stars 173 forks source link

Support for draft 2020-12 array syntax #447

Open JensenLenard opened 1 year ago

JensenLenard commented 1 year ago

In the latest json schema draft the syntax has changed for arrays and how additional items are being defined. See: https://json-schema.org/draft/2020-12/release-notes.html

As I did not find an open issue regarding that, I wanted to ask if there is currently work being done to support the new syntax, or if it's still open. Alternatively I wanted to ask if there is a workaround to use this draft version, in case these changes are not wanted for this library.

On a first look through the library it appeared like only this method needed to be changed:

  private addProperty() {
    let itemSchema = this.schema.items
    if (Array.isArray(this.schema.items)) {
      const itemSchemas = this.schema.items as object[]
      if (itemSchemas.length > (<FormProperty[]>this.properties).length) {
        itemSchema = itemSchema[(<FormProperty[]>this.properties).length]
      } else if (this.schema.additionalItems) {
        itemSchema = this.schema.additionalItems
      } else {
        // souldn't add new items since schema is undefined for the item at its position
        return null
      }
    }
    let newProperty = this.formPropertyFactory.createProperty(itemSchema, this);
    (<FormProperty[]>this.properties).push(newProperty);
    return newProperty;
  }

It may be enough to just use "prefixItems" instead of "items" and "items" instead of "additionalItems" to implement the new changes, as it appears that they are effectively just renamed in the new draft.