class-validator-jsonschema
class-validator-jsonschema copied to clipboard
Move inherited properties before sub class properties
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