UNPKG

mindee

Version:

Mindee Client Library for Node.js

89 lines (88 loc) 3.09 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DataSchema = exports.DataSchemaReplace = exports.DataSchemaField = void 0; const errors_1 = require("../errors"); class DataSchemaField { constructor(fields) { this.name = fields["name"]; this.title = fields["title"]; this.isArray = fields["is_array"]; this.type = fields["type"]; this.classificationValues = fields["classification_values"]; this.uniqueValues = fields["unique_values"]; this.description = fields["description"]; this.guidelines = fields["guidelines"]; this.nestedFields = fields["nested_fields"]; } toJSON() { const out = { name: this.name, title: this.title, // eslint-disable-next-line @typescript-eslint/naming-convention,camelcase is_array: this.isArray, type: this.type, }; // eslint-disable-next-line camelcase if (this.classificationValues !== undefined) out.classification_values = this.classificationValues; // eslint-disable-next-line camelcase if (this.uniqueValues !== undefined) out.unique_values = this.uniqueValues; if (this.description !== undefined) out.description = this.description; if (this.guidelines !== undefined) out.guidelines = this.guidelines; // eslint-disable-next-line camelcase if (this.nestedFields !== undefined) out.nested_fields = this.nestedFields; return out; } toString() { return JSON.stringify(this.toJSON()); } } exports.DataSchemaField = DataSchemaField; /** * The structure to completely replace the data schema of the model. */ class DataSchemaReplace { constructor(dataSchemaReplace) { if (!dataSchemaReplace || !dataSchemaReplace.fields) { throw new errors_1.MindeeError("Invalid Data Schema provided."); } if (dataSchemaReplace["fields"].length === 0) { throw new TypeError("Data Schema replacement fields cannot be empty."); } this.fields = dataSchemaReplace["fields"].map((field) => (new DataSchemaField(field))); } toJSON() { return { fields: this.fields.map(e => e.toJSON()) }; } toString() { return JSON.stringify(this.toJSON()); } } exports.DataSchemaReplace = DataSchemaReplace; /** * Modify the Data Schema. */ class DataSchema { constructor(dataSchema) { if (typeof dataSchema === "string") { this.replace = new DataSchemaReplace(JSON.parse(dataSchema)["replace"]); } else if (dataSchema["replace"] instanceof DataSchemaReplace) { this.replace = dataSchema["replace"]; } else { this.replace = new DataSchemaReplace(dataSchema["replace"]); } } toJSON() { return { replace: this.replace?.toJSON() }; } toString() { return JSON.stringify(this.toJSON()); } } exports.DataSchema = DataSchema;