@digitalasset/daml-ledger
Version:
DAML Ledger API Node.js bindings
54 lines • 1.94 kB
JavaScript
;
// Copyright (c) 2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
Object.defineProperty(exports, "__esModule", { value: true });
exports.union = void 0;
const Validation_1 = require("./Validation");
const util_1 = require("util");
function union(type, tag, values) {
return {
tag: tag,
type: type,
values: values,
validate(value, key, tree) {
const node = Validation_1.init(key, tree);
const actualType = Validation_1.typeOf(value);
if (actualType !== 'object') {
node.errors.push({
errorType: 'type-error',
expectedType: type,
actualType: actualType
});
return node;
}
const typeTags = values();
const typeTag = value[this.tag];
if (typeTag in typeTags) {
const validation = typeTags[typeTag];
if (key && tree) {
return validation.validate(value, key, tree);
}
else {
return validation.validate(value);
}
}
else if (typeTag === null || typeTag === undefined) {
node.errors.push({
errorType: 'missing-type-tag',
expectedTypeTags: Object.keys(typeTags)
});
return node;
}
else {
node.errors.push({
errorType: 'unexpected-type-tag',
expectedTypeTags: Object.keys(typeTags),
actualTypeTag: typeof typeTag === 'string' ? typeTag : util_1.inspect(typeTag)
});
return node;
}
}
};
}
exports.union = union;
//# sourceMappingURL=Union.js.map