Skip to content

Commit 3374370

Browse files
authoredNov 13, 2023
JsonSchema: Fix @maxValueExclusive setting minimumExclusive instead of maximumExclusive (microsoft#2654)
fix [microsoft#2531](microsoft#2531)
1 parent 797baa8 commit 3374370

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@typespec/json-schema",
5+
"comment": "JsonSchema: Fix `@maxValueExclusive` setting `minimumExclusive` instead of `maximumExclusive`",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@typespec/json-schema"
10+
}

‎packages/json-schema/src/json-schema-emitter.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ export class JsonSchemaEmitter extends TypeEmitter<Record<string, any>, JSONSche
451451
applyConstraint(getMinValue, "minimum");
452452
applyConstraint(getMinValueExclusive, "exclusiveMinimum");
453453
applyConstraint(getMaxValue, "maximum");
454-
applyConstraint(getMaxValueExclusive, "exclusiveMinimum");
454+
applyConstraint(getMaxValueExclusive, "exclusiveMaximum");
455455
applyConstraint(getPattern, "pattern");
456456
applyConstraint(getMinItems, "minItems");
457457
applyConstraint(getMaxItems, "maxItems");

‎packages/json-schema/test/scalar-constraints.test.ts

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import assert from "assert";
1+
import assert, { strictEqual } from "assert";
22
import { emitSchema } from "./utils.js";
33

44
describe("jsonschema: scalar constraints", () => {
@@ -53,6 +53,36 @@ describe("jsonschema: scalar constraints", () => {
5353
});
5454
});
5555

56+
describe("@minValueExclusive/@maxValueExclusive", () => {
57+
for (const numType of scalarNumberTypes) {
58+
it(numType, async () => {
59+
const schemas = await emitSchema(
60+
`
61+
@minValueExclusive(1)
62+
@maxValueExclusive(2)
63+
scalar Test extends ${numType};
64+
`
65+
);
66+
67+
strictEqual(schemas["Test.json"].exclusiveMinimum, 1);
68+
strictEqual(schemas["Test.json"].exclusiveMaximum, 2);
69+
});
70+
71+
it("can be applied on a union", async () => {
72+
const schemas = await emitSchema(
73+
`
74+
@minValueExclusive(1)
75+
@maxValueExclusive(2)
76+
union Test {int32, string, null};
77+
`
78+
);
79+
80+
strictEqual(schemas["Test.json"].exclusiveMinimum, 1);
81+
strictEqual(schemas["Test.json"].exclusiveMaximum, 2);
82+
});
83+
}
84+
});
85+
5686
describe("on property", () => {
5787
for (const numType of [...scalarNumberTypes, "int32 | string | null"]) {
5888
it(`handles ${numType} properties`, async () => {

0 commit comments

Comments
 (0)
Please sign in to comment.