UNPKG

@tsed/schema

Version:
46 lines (45 loc) 1.75 kB
import { __decorate } from "tslib"; import { DecoratorTypes, isClass, Metadata, prototypeOf } from "@tsed/core"; import { JsonEntityComponent } from "../decorators/config/jsonEntityComponent.js"; import { JsonEntityStore } from "./JsonEntityStore.js"; import { JsonSchema } from "./JsonSchema.js"; let JsonPropertyStore = class JsonPropertyStore extends JsonEntityStore { constructor() { super(...arguments); this.parent = JsonEntityStore.from(this.target); } static get(target, propertyKey) { return JsonEntityStore.from(prototypeOf(target), propertyKey); } build() { if (!this._type) { this.buildType(Metadata.getType(prototypeOf(this.target), this.propertyKey)); } this._type = this._type || Object; const properties = this.parent.schema.get("properties"); let schema = properties[this.propertyName]; if (!schema) { this.parent.children.set(this.propertyName, this); if (this.isCollection) { schema = JsonSchema.from({ type: this.collectionType }); schema.itemSchema(this.type); } else if (isClass(this.type)) { schema = JsonSchema.from({ type: "object" }); schema.itemSchema(this.type); } else { schema = JsonSchema.from({ type: this.type }); } } this.parent.schema.addProperty(this.propertyName, schema); this._schema = schema; } }; JsonPropertyStore = __decorate([ JsonEntityComponent(DecoratorTypes.PROP) ], JsonPropertyStore); export { JsonPropertyStore }; export const PropertyMetadata = JsonPropertyStore;