class-validator-jsonschema icon indicating copy to clipboard operation
class-validator-jsonschema copied to clipboard

Move inherited properties before sub class properties

Open chris-schra opened this issue 2 years ago • 0 comments

Imagine classes like these

@JSONSchema()
class RichTextSchema extends BaseContentSchema {
  static collectionName: SchemaCollectionName = {
    plural: "RichTexts",
    singular: "RichText",
  };

  @IsOptional()
  @IsString()
  text: string;
}
@JSONSchema()
class TextImageSchema extends RichTextSchema {
  @IsOptional()
  @IsString()
  image: string;
}

By the way "metas" are currently built

    const metas = ownMetas
      .concat(getInheritedMetadatas(target, metadatas))
      .filter(
        (propMeta) =>
          !(
            isExcluded(propMeta, options) ||
            isExcluded({ ...propMeta, target }, options)
          )
      )

Property "image" of sub class TextImageSchema will be output before "text" of parent class RichTextSchema.

I suggest there should be at least an option to be able to output inherited metas before own metas, like

    const allMetas = options.inheritedPropertiesFirst ? 
        getInheritedMetadatas(target, metadatas).concat(ownMetas) : 
        ownMetas.concat(getInheritedMetadatas(target, metadatas))
    
    const metas = allMetas
      .filter(
        (propMeta) =>
          !(
            isExcluded(propMeta, options) ||
            isExcluded({ ...propMeta, target }, options)
          )
      )

In our use case it makes more sense, because we achieve the same "ordering" of properties which helps visualizing them in documentation and react-json-schema-forms

chris-schra avatar Jun 26 '22 08:06 chris-schra