diffusion
Version:
Diffusion JavaScript client
177 lines (176 loc) • 7.44 kB
JavaScript
;
/**
* @module diffusion.datatypes.RecordV2
*/
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RecordModelImpl = void 0;
var errors_1 = require("./../../../../errors/errors");
var abstract_record_model_1 = require("./../../../data/record/model/abstract-record-model");
var field_impl_1 = require("./../../../data/record/schema/field-impl");
var require_non_null_1 = require("./../../../util/require-non-null");
/**
* Validate Record V2 model data against a schema
*
* @param schema the schema to validate against
* @param model the record data
* @throws an {@link InvalidDataError} if the model data is found to be invalid
*/
function validateModel(schema, model) {
var e_1, _a;
var modelSize = model.length;
var lastRecord = schema.lastRecord();
var lastIndex = lastRecord.index;
var lastMax = lastRecord.max;
if (lastMax !== -1 && modelSize > lastIndex + lastMax) {
throw new errors_1.InvalidDataError("Too many occurrences of record '" + lastRecord.name + "'");
}
if (modelSize < lastIndex + lastRecord.min) {
throw new errors_1.InvalidDataError('Too few record occurrences');
}
var schemaRecords = schema.records();
try {
for (var schemaRecords_1 = __values(schemaRecords), schemaRecords_1_1 = schemaRecords_1.next(); !schemaRecords_1_1.done; schemaRecords_1_1 = schemaRecords_1.next()) {
var schemaRecord = schemaRecords_1_1.value;
var startIndex = schemaRecord.index;
var min = schemaRecord.min;
for (var i = startIndex; i < startIndex + min; ++i) {
validateRecord(schemaRecord, i - startIndex, model[i]);
}
// process variable records to the end
if (schemaRecord.isVariable) {
for (var i = startIndex + min; i < modelSize; ++i) {
var index = i - startIndex;
validateRecord(schemaRecord, index, model[i]);
}
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (schemaRecords_1_1 && !schemaRecords_1_1.done && (_a = schemaRecords_1.return)) _a.call(schemaRecords_1);
}
finally { if (e_1) throw e_1.error; }
}
}
/**
* Validate Record V2 model data of a single record against the schema record
*
* @param schemaRecord the record schema to validate against
* @param recordIndex the record index
* @param record the individual record data
* @throws an error if the model data is found to be invalid
*/
function validateRecord(schemaRecord, recordIndex, record) {
var e_2, _a;
var recordSize = record.length;
var lastField = schemaRecord.lastField();
var lastMax = lastField.max;
var lastIndex = lastField.index;
if (lastMax !== -1 && recordSize > lastIndex + lastMax) {
throw new errors_1.InvalidDataError("Too many occurrences of field '" + schemaRecord.name + "(" + recordIndex + ")." + lastField.name + "'");
}
if (recordSize < lastIndex + lastField.min) {
throw new errors_1.InvalidDataError("Too few field occurrences in record '" + schemaRecord.name + "(" + recordIndex + ")'");
}
try {
for (var _b = __values(schemaRecord.fields), _c = _b.next(); !_c.done; _c = _b.next()) {
var schemaField = _c.value;
var startIndex = schemaField.index;
var min = schemaField.min;
for (var i = startIndex; i < startIndex + min; ++i) {
validateField(schemaRecord, recordIndex, schemaField, i - startIndex, record[i]);
}
if (schemaField.isVariable) {
for (var i = startIndex + min; i < recordSize; i++) {
var index = i - startIndex;
validateField(schemaRecord, recordIndex, schemaField, index, record[i]);
}
}
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_2) throw e_2.error; }
}
}
/**
* Validate Record V2 model data of a single field against the schema field
*
* @param schemaRecord the record schema to validate against
* @param recordIndex the record index
* @param schemaRecord the field schema to validate against
* @param index the field index
* @param value the field model data
*
* @throws an {@link InvalidDataError} if the model data is found to be invalid
*/
function validateField(schemaRecord, recordIndex, schemaField, index, value) {
try {
field_impl_1.normalise(schemaField, require_non_null_1.requireNonNull(value, 'FieldData'));
}
catch (e) {
throw new errors_1.InvalidDataError("Invalid value for field '" + schemaRecord.name + "(" + recordIndex + ")." + schemaField.name + "(" + index + ")");
}
}
/**
* Implementation of {@link AbstractRecordModel} for an immutable record model
*/
var RecordModelImpl = /** @class */ (function (_super) {
__extends(RecordModelImpl, _super);
/**
* Create a RecordModelImpl
*
* @param recordV2Constructor a constructor function for creating {@link RecordV2} implementations
* @param schema the schema that this model corresponds to
* @param recordData the record data
* @param validate a flag indicating whether to validate the record data against the schema
*
* @throws an {@link InvalidDataError} if `validate==true` and the model data is found to be invalid
*/
function RecordModelImpl(recordV2Constructor, schema, recordData, validate) {
var _this = _super.call(this, recordV2Constructor, schema) || this;
if (validate) {
validateModel(schema, recordData);
}
_this.recordData = recordData;
return _this;
}
/**
* @inheritdoc
*/
RecordModelImpl.prototype.model = function () {
return Array.from(this.recordData);
};
return RecordModelImpl;
}(abstract_record_model_1.AbstractRecordModel));
exports.RecordModelImpl = RecordModelImpl;