@digitalasset/daml-ledger
Version:
DAML Ledger API Node.js bindings
31 lines • 1.16 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.enumeration = void 0;
const Validation_1 = require("./Validation");
function valuesOf(enumeration) {
return Object.keys(enumeration).filter(k => typeof enumeration[k] === "number").map(k => enumeration[k]);
}
// NOTE: works only for `enum`s without custom initializers
function enumeration(enumeration, type) {
return {
type: type,
validate(value, key, validation) {
const node = Validation_1.init(key, validation);
const values = valuesOf(enumeration);
if (!values.some(v => v === value)) {
const error = {
errorType: 'type-error',
expectedType: this.type,
actualType: Validation_1.typeOf(value)
};
node.errors.push(error);
}
return validation || node;
}
};
}
exports.enumeration = enumeration;
;
//# sourceMappingURL=Enumeration.js.map