UNPKG

vulcain-corejs

Version:
59 lines (57 loc) 2.21 kB
"use strict"; class SchemaVisitor { constructor(domain, visitor) { this.domain = domain; this.visitor = visitor; } visit(schemaName, entity) { let schema = this.domain.resolveSchemaDescription(schemaName); if (this.visitor.visitEntity && !this.visitor.visitEntity(entity, schema)) return; let sch = schema; while (sch) { for (const ps in sch.properties) { if (!sch.properties.hasOwnProperty(ps)) continue; let prop = sch.properties[ps]; if (prop) { prop.name = ps; let val = entity[ps]; this.visitor.visitProperty && this.visitor.visitProperty(val, prop); } } sch = sch.extends && this.domain.resolveSchemaDescription(sch.extends); } sch = schema; while (sch) { for (const ref in schema.references) { if (!schema.references.hasOwnProperty(ref)) continue; let relationshipSchema = schema.references[ref]; let refValue = entity[ref]; if (relationshipSchema && refValue) { let item = relationshipSchema.item; if (item === "any" && refValue && refValue.__schema) { item = refValue.__schema; } let elemSchema = this.domain.findSchemaDescription(item); if (!elemSchema && item !== "any") { continue; } elemSchema.name = ref; if (Array.isArray(refValue)) { for (let elem of refValue) { this.visit(elemSchema, elem); } } else { this.visit(elemSchema, refValue); } } } sch = sch.extends && this.domain.resolveSchemaDescription(sch.extends); } } } exports.SchemaVisitor = SchemaVisitor; //# sourceMappingURL=visitor.js.map