Skip to content

Commit

Permalink
fixup! WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ezzatron committed Mar 24, 2024
1 parent 95718a7 commit 2fa9090
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/declaration/network-port-number.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import type {
Constraint,
DeclarationConstraintOptions,
} from "../constraint.js";
import { createNetworkPortNumberConstraint } from "../constraint/network-port-number.js";
import {
assertRangeSpec,
Expand All @@ -23,6 +27,7 @@ import { resolve } from "../maybe.js";
import { ScalarSchema, createScalar, toString } from "../schema.js";

export type Options = DeclarationOptions<number> &
DeclarationConstraintOptions<number> &
DeclarationExampleOptions<number> &
Partial<RangeConstraintSpec<number>>;

Expand Down Expand Up @@ -62,7 +67,10 @@ function createSchema(name: string, options: Options): ScalarSchema<number> {
return Number(v);
}

const constraints = [createNetworkPortNumberConstraint()];
const { constraints: explicitConstraints = [] } = options;
const constraints: Constraint<number>[] = [
createNetworkPortNumberConstraint(),
];

try {
if (hasNumberRangeConstraint(options)) {
Expand All @@ -73,7 +81,10 @@ function createSchema(name: string, options: Options): ScalarSchema<number> {
throw new SpecError(name, normalize(error));
}

return createScalar("port number", toString, unmarshal, constraints);
return createScalar("port number", toString, unmarshal, [
...constraints,
...explicitConstraints,
]);
}

function buildExamples(): Example<number>[] {
Expand Down
68 changes: 68 additions & 0 deletions test/suite/declaration/network-port-number.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,4 +298,72 @@ describe("Network port number declarations", () => {
});
},
);

describe("when the declaration has constraints", () => {
beforeEach(() => {
declaration = networkPortNumber("AUSTENITE_PORT", "<description>", {
constraints: [
{
description: "<constraint A>",
constrain: (v) => v % 2 === 0 || "must be divisible by 2",
},
{
description: "<constraint B>",
constrain: (v) => v % 3 === 0 || "must be divisible by 3",
},
],
examples: [{ value: 6, label: "example" }],
});
});

describe("when the value satisfies the constraints", () => {
beforeEach(() => {
process.env.AUSTENITE_PORT = "6";

initialize({ onInvalid: noop });
});

describe(".value()", () => {
it("returns the value", () => {
expect(declaration.value()).toBe(6);
});
});
});

describe("when the value violates the first constraint", () => {
beforeEach(() => {
process.env.AUSTENITE_PORT = "3";

initialize({ onInvalid: noop });
});

describe(".value()", () => {
it("throws", () => {
expect(() => {
declaration.value();
}).toThrow(
"value of AUSTENITE_PORT (3) is invalid: must be divisible by 2",
);
});
});
});

describe("when the value violates the second constraint", () => {
beforeEach(() => {
process.env.AUSTENITE_PORT = "2";

initialize({ onInvalid: noop });
});

describe(".value()", () => {
it("throws", () => {
expect(() => {
declaration.value();
}).toThrow(
"value of AUSTENITE_PORT (2) is invalid: must be divisible by 3",
);
});
});
});
});
});

0 comments on commit 2fa9090

Please sign in to comment.