UNPKG

zod-opts

Version:

node.js CLI option parser / validator using Zod

80 lines 4.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const internal_parser_1 = require("../src/internal_parser"); const validator_1 = require("../src/validator"); const test_util_1 = require("./test_util"); describe("validateCandidateValue()", () => { test("valid", () => { expect((0, validator_1.validateCandidateValue)((0, test_util_1.createInternalOption)({ type: "string" }), "str", false)).toEqual({ value: "str", }); expect((0, validator_1.validateCandidateValue)((0, test_util_1.createInternalOption)({ type: "string" }), "", false)).toEqual({ value: "", }); expect((0, validator_1.validateCandidateValue)((0, test_util_1.createInternalOption)({ type: "number" }), "10", false)).toEqual({ value: 10, }); expect((0, validator_1.validateCandidateValue)((0, test_util_1.createInternalOption)({ type: "number" }), "-10.1", false)).toEqual({ value: -10.1, }); expect((0, validator_1.validateCandidateValue)((0, test_util_1.createInternalOption)({ type: "boolean" }), undefined, false)).toEqual({ value: true, }); expect((0, validator_1.validateCandidateValue)((0, test_util_1.createInternalOption)({ type: "boolean" }), undefined, true)).toEqual({ value: false, }); }); test("invalid", () => { expect((0, validator_1.validateCandidateValue)((0, test_util_1.createInternalOption)({ type: "string" }), undefined, false)).toEqual(undefined); expect((0, validator_1.validateCandidateValue)((0, test_util_1.createInternalOption)({ type: "number" }), undefined, false)).toEqual(undefined); expect((0, validator_1.validateCandidateValue)((0, test_util_1.createInternalOption)({ type: "number" }), "str", false)).toEqual(undefined); expect((0, validator_1.validateCandidateValue)((0, test_util_1.createInternalOption)({ type: "boolean" }), "str", false)).toEqual(undefined); }); }); describe("validatePositionalCandidateValue()", () => { test("valid", () => { expect((0, validator_1.validatePositionalCandidateValue)((0, test_util_1.createInternalPositionalArgument)({ type: "string" }), "str")).toEqual({ value: "str", }); expect((0, validator_1.validatePositionalCandidateValue)((0, test_util_1.createInternalPositionalArgument)({ type: "number" }), "10")).toEqual({ value: 10, }); expect((0, validator_1.validatePositionalCandidateValue)((0, test_util_1.createInternalPositionalArgument)({ type: "number" }), "-10.1")).toEqual({ value: -10.1, }); expect((0, validator_1.validatePositionalCandidateValue)((0, test_util_1.createInternalPositionalArgument)({ type: "string", isArray: true }), ["str1", "str2"])).toEqual({ value: ["str1", "str2"], }); expect((0, validator_1.validatePositionalCandidateValue)((0, test_util_1.createInternalPositionalArgument)({ type: "number", isArray: true }), ["10", "-10.1"])).toEqual({ value: [10, -10.1], }); }); test("invalid", () => { expect((0, validator_1.validatePositionalCandidateValue)((0, test_util_1.createInternalPositionalArgument)({ type: "string" }), undefined)).toEqual(undefined); expect((0, validator_1.validatePositionalCandidateValue)((0, test_util_1.createInternalPositionalArgument)({ type: "number" }), undefined)).toEqual(undefined); expect((0, validator_1.validatePositionalCandidateValue)((0, test_util_1.createInternalPositionalArgument)({ type: "string" }), "")).toEqual({ value: "" }); expect((0, validator_1.validatePositionalCandidateValue)((0, test_util_1.createInternalPositionalArgument)({ type: "number" }), "str")).toEqual(undefined); expect((0, validator_1.validatePositionalCandidateValue)((0, test_util_1.createInternalPositionalArgument)({ type: "number", isArray: true }), ["str"])).toEqual(undefined); expect((0, validator_1.validatePositionalCandidateValue)((0, test_util_1.createInternalPositionalArgument)({ type: "number", isArray: true }), ["10", ""])).toEqual(undefined); }); }); describe("validate()", () => { test("returns validated result", () => { const params = { options: [(0, test_util_1.createInternalOption)({ name: "opt1" })], positionalArgs: [], args: ["--opt1", "opt_str1"], }; const parsed = (0, internal_parser_1.parse)(params); expect((0, validator_1.validate)(parsed, params.options, params.positionalArgs)).toEqual({ options: [ { name: "opt1", value: "opt_str1", }, ], positionalArgs: [], }); }); }); //# sourceMappingURL=validator.test.js.map