Skip to content

Commit

Permalink
JsonSchema: Fix @maxValueExclusive setting minimumExclusive inste…
Browse files Browse the repository at this point in the history
…ad of `maximumExclusive` (#2654)

fix [#2531](#2531)
  • Loading branch information
timotheeguerin authored Nov 13, 2023
1 parent 797baa8 commit 3374370
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@typespec/json-schema",
"comment": "JsonSchema: Fix `@maxValueExclusive` setting `minimumExclusive` instead of `maximumExclusive`",
"type": "none"
}
],
"packageName": "@typespec/json-schema"
}
2 changes: 1 addition & 1 deletion packages/json-schema/src/json-schema-emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ export class JsonSchemaEmitter extends TypeEmitter<Record<string, any>, JSONSche
applyConstraint(getMinValue, "minimum");
applyConstraint(getMinValueExclusive, "exclusiveMinimum");
applyConstraint(getMaxValue, "maximum");
applyConstraint(getMaxValueExclusive, "exclusiveMinimum");
applyConstraint(getMaxValueExclusive, "exclusiveMaximum");
applyConstraint(getPattern, "pattern");
applyConstraint(getMinItems, "minItems");
applyConstraint(getMaxItems, "maxItems");
Expand Down
32 changes: 31 additions & 1 deletion packages/json-schema/test/scalar-constraints.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import assert from "assert";
import assert, { strictEqual } from "assert";
import { emitSchema } from "./utils.js";

describe("jsonschema: scalar constraints", () => {
Expand Down Expand Up @@ -53,6 +53,36 @@ describe("jsonschema: scalar constraints", () => {
});
});

describe("@minValueExclusive/@maxValueExclusive", () => {
for (const numType of scalarNumberTypes) {
it(numType, async () => {
const schemas = await emitSchema(
`
@minValueExclusive(1)
@maxValueExclusive(2)
scalar Test extends ${numType};
`
);

strictEqual(schemas["Test.json"].exclusiveMinimum, 1);
strictEqual(schemas["Test.json"].exclusiveMaximum, 2);
});

it("can be applied on a union", async () => {
const schemas = await emitSchema(
`
@minValueExclusive(1)
@maxValueExclusive(2)
union Test {int32, string, null};
`
);

strictEqual(schemas["Test.json"].exclusiveMinimum, 1);
strictEqual(schemas["Test.json"].exclusiveMaximum, 2);
});
}
});

describe("on property", () => {
for (const numType of [...scalarNumberTypes, "int32 | string | null"]) {
it(`handles ${numType} properties`, async () => {
Expand Down

0 comments on commit 3374370

Please sign in to comment.