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 2fa9090 commit fb7a537
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/declaration/number.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import type {
Constraint,
DeclarationConstraintOptions,
} from "../constraint.js";
import {
createRangeConstraint,
hasNumberRangeConstraint,
Expand All @@ -21,6 +25,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 @@ -59,7 +64,8 @@ function createSchema(name: string, options: Options): ScalarSchema<number> {
return n;
}

const constraints = [];
const { constraints: explicitConstraints = [] } = options;
const constraints: Constraint<number>[] = [];

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

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

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

describe("when the declaration has constraints", () => {
beforeEach(() => {
declaration = number("AUSTENITE_NUMBER", "<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_NUMBER = "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_NUMBER = "3";

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

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

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

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

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

0 comments on commit fb7a537

Please sign in to comment.