@shapediver/sdk.sdtf-v1
Version:
Official sdTF SDK for TypeScript
118 lines • 7.64 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SdtfComponentValidator = void 0;
const sdk_sdtf_core_1 = require("@shapediver/sdk.sdtf-core");
class SdtfComponentValidator {
constructor(componentList) {
this.componentList = componentList;
}
validateAccessor(accessor) {
if (!(0, sdk_sdtf_core_1.isUint)(accessor.bufferView))
throw new sdk_sdtf_core_1.SdtfError("Invalid accessor: Required property 'bufferView' must be an unsigned integer.");
if (accessor.bufferView >= this.componentList.bufferViews.length)
throw new sdk_sdtf_core_1.SdtfError('Invalid accessor: Buffer view index is out of range.');
}
validateAsset(asset) {
if (!(0, sdk_sdtf_core_1.isUint)(asset.fileInfo))
throw new sdk_sdtf_core_1.SdtfError("Invalid asset: Required property 'fileInfo' must be an unsigned integer.");
if (asset.fileInfo !== 0)
throw new sdk_sdtf_core_1.SdtfError('Invalid asset: Type hint index is out of range.');
}
validateAttributes(attributes) {
if (!attributes.entries)
throw new sdk_sdtf_core_1.SdtfError("Invalid attributes: Required property 'entries' must be a string-keyed object.");
Object.values(attributes.entries).forEach((attribute) => {
if (!(0, sdk_sdtf_core_1.isUint)(attribute.typeHint))
throw new sdk_sdtf_core_1.SdtfError("Invalid attribute: Required property 'typeHint' must be an unsigned integer.");
if (attribute.accessor && !(0, sdk_sdtf_core_1.isUint)(attribute.accessor))
throw new sdk_sdtf_core_1.SdtfError("Invalid attribute: Optional property 'accessor' must be an unsigned integer.");
if (attribute.accessor && attribute.accessor >= this.componentList.accessors.length)
throw new sdk_sdtf_core_1.SdtfError('Invalid attribute: Accessor index is out of range.');
if (attribute.typeHint && attribute.typeHint >= this.componentList.typeHints.length)
throw new sdk_sdtf_core_1.SdtfError('Invalid attribute: Type hint index is out of range.');
});
}
validateBuffer(buffer) {
if (!(0, sdk_sdtf_core_1.isUint)(buffer.byteLength))
throw new sdk_sdtf_core_1.SdtfError("Invalid buffer: Required property 'byteLength' must be an unsigned integer.");
}
validateBufferView(bufferView) {
if (!(0, sdk_sdtf_core_1.isUint)(bufferView.buffer))
throw new sdk_sdtf_core_1.SdtfError("Invalid buffer view: Required property 'buffer' must be an unsigned integer.");
if (!(0, sdk_sdtf_core_1.isUint)(bufferView.byteLength))
throw new sdk_sdtf_core_1.SdtfError("Invalid buffer view: Required property 'byteLength' must be an unsigned integer.");
if (!(0, sdk_sdtf_core_1.isUint)(bufferView.byteOffset))
throw new sdk_sdtf_core_1.SdtfError("Invalid buffer view: Required property 'byteOffset' must be an unsigned integer.");
if (!(0, sdk_sdtf_core_1.isNonEmptyString)(bufferView.contentType))
throw new sdk_sdtf_core_1.SdtfError("Invalid buffer view: Required property 'contentType' must be a non-empty string.");
if (bufferView.buffer >= this.componentList.buffers.length)
throw new sdk_sdtf_core_1.SdtfError('Invalid buffer view: Buffer index is out of range.');
}
validateChunk(chunk) {
try {
this.validateChunkOrNode(chunk);
}
catch (e) {
throw new sdk_sdtf_core_1.SdtfError(`Invalid chunk: ${e.message}`);
}
if (typeof chunk.name !== 'string')
throw new sdk_sdtf_core_1.SdtfError("Invalid chunk: Required property 'name' must be a string.");
}
validateDataItem(dataItem) {
if (!(0, sdk_sdtf_core_1.isUint)(dataItem.typeHint))
throw new sdk_sdtf_core_1.SdtfError("Invalid item: Required property 'typeHint' must be an unsigned integer.");
if (dataItem.accessor && !(0, sdk_sdtf_core_1.isUint)(dataItem.accessor))
throw new sdk_sdtf_core_1.SdtfError("Invalid item: Optional property 'accessor' must be an unsigned integer.");
if (dataItem.attributes && !(0, sdk_sdtf_core_1.isUint)(dataItem.attributes))
throw new sdk_sdtf_core_1.SdtfError("Invalid item: Optional property 'attributes' must be an unsigned integer.");
if (dataItem.accessor && dataItem.accessor >= this.componentList.accessors.length)
throw new sdk_sdtf_core_1.SdtfError('Invalid item: Accessor index is out of range.');
if (dataItem.attributes && dataItem.attributes >= this.componentList.attributes.length)
throw new sdk_sdtf_core_1.SdtfError('Invalid item: Attributes index is out of range.');
if (dataItem.typeHint && dataItem.typeHint >= this.componentList.typeHints.length)
throw new sdk_sdtf_core_1.SdtfError('Invalid item: Type hint index is out of range.');
}
validateFileInfo(fileInfo) {
if (!(0, sdk_sdtf_core_1.isNonEmptyString)(fileInfo.version))
throw new sdk_sdtf_core_1.SdtfError("Invalid file info: Required property 'version' must be a non-empty string.");
}
validateNode(node) {
try {
this.validateChunkOrNode(node);
}
catch (e) {
throw new sdk_sdtf_core_1.SdtfError(`Invalid node: ${e.message}`);
}
}
validateTypeHint(typeHint) {
if (!(0, sdk_sdtf_core_1.isNonEmptyString)(typeHint.name))
throw new sdk_sdtf_core_1.SdtfError("Invalid type hint: Required property 'name' must be a non-empty string.");
}
validateChunkOrNode(chunkOrNode) {
if (chunkOrNode.attributes && !(0, sdk_sdtf_core_1.isUint)(chunkOrNode.attributes))
throw new sdk_sdtf_core_1.SdtfError("Optional property 'attributes' must be an unsigned integer.");
if (!(0, sdk_sdtf_core_1.isUintArray)(chunkOrNode.items))
throw new sdk_sdtf_core_1.SdtfError("Required property 'items' must be an array of unsigned integers.");
if (!(0, sdk_sdtf_core_1.isUintArray)(chunkOrNode.nodes))
throw new sdk_sdtf_core_1.SdtfError("Required property 'nodes' must be an array of unsigned integers.");
if (chunkOrNode.typeHint && !(0, sdk_sdtf_core_1.isUint)(chunkOrNode.typeHint))
throw new sdk_sdtf_core_1.SdtfError("Optional property 'typeHint' must be an unsigned integer.");
if (chunkOrNode.attributes &&
chunkOrNode.attributes >= this.componentList.attributes.length)
throw new sdk_sdtf_core_1.SdtfError('Attributes index is out of range.');
chunkOrNode.items.forEach((itemIndex) => {
if (itemIndex >= this.componentList.items.length)
throw new sdk_sdtf_core_1.SdtfError(`Node index '${itemIndex}' is out of range.`);
});
chunkOrNode.nodes.forEach((nodeIndex) => {
if (nodeIndex >= this.componentList.nodes.length)
throw new sdk_sdtf_core_1.SdtfError(`Node index '${nodeIndex}' is out of range.`);
});
if (chunkOrNode.typeHint && chunkOrNode.typeHint >= this.componentList.typeHints.length)
throw new sdk_sdtf_core_1.SdtfError('Type hint index is out of range.');
if (!chunkOrNode.nodes.every((nodePos) => this.componentList.nodes[nodePos].componentId !== chunkOrNode.componentId))
throw new sdk_sdtf_core_1.SdtfError("Node is referencing itself in the 'nodes' property.");
}
}
exports.SdtfComponentValidator = SdtfComponentValidator;
//# sourceMappingURL=SdtfComponentValidator.js.map