forked from microsoft/typespec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscalar-constraints.test.ts
180 lines (162 loc) · 4.81 KB
/
scalar-constraints.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import assert, { strictEqual } from "assert";
import { describe, it } from "vitest";
import { emitSchema } from "./utils.js";
describe("jsonschema: scalar constraints", () => {
describe("numeric constraints", () => {
const scalarNumberTypes = [
"int8",
"int16",
"int32",
"uint8",
"uint16",
"uint32",
"integer",
"float32",
"float64",
"numeric",
"float",
"safeint",
];
function assertNumericConstraints(schema: any) {
assert.strictEqual(schema.minimum, 1);
assert.strictEqual(schema.maximum, 2);
assert.strictEqual(schema.multipleOf, 10);
}
describe("on scalar declaration", () => {
for (const numType of scalarNumberTypes) {
it(`handles ${numType}`, async () => {
const schemas = await emitSchema(`
@minValue(1)
@maxValue(2)
@multipleOf(10)
scalar Test extends ${numType};
`);
assertNumericConstraints(schemas["Test.json"]);
});
}
it("on a union", async () => {
const schemas = await emitSchema(`
@minValue(1)
@maxValue(2)
@multipleOf(10)
union Test {
int32,
string,
null
};
`);
assertNumericConstraints(schemas["Test.json"]);
});
});
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 () => {
const schemas = await emitSchema(`
model Test {
@minValue(1)
@maxValue(2)
@multipleOf(10)
prop: ${numType};
}
`);
assertNumericConstraints(schemas["Test.json"].properties.prop);
});
}
});
});
describe("string constraints", () => {
function assertStringConstraints(schema: any) {
assert.strictEqual(schema.minLength, 1);
assert.strictEqual(schema.maxLength, 2);
assert.strictEqual(schema.pattern, "a|b");
assert.strictEqual(schema.format, "ipv4");
assert.strictEqual(schema.contentEncoding, "base64url");
assert.strictEqual(schema.contentMediaType, "application/jwt");
assert.deepStrictEqual(schema.contentSchema, {
$ref: "JwtToken.json",
});
}
it("on scalar declaration", async () => {
const schemas = await emitSchema(`
@minLength(1)
@maxLength(2)
@pattern("a|b")
@format("ipv4")
@contentEncoding("base64url")
@contentMediaType("application/jwt")
@contentSchema(JwtToken)
scalar shortString extends string;
model JwtToken is Array<Record<string>>;
`);
assertStringConstraints(schemas["shortString.json"]);
});
it("on union", async () => {
const schemas = await emitSchema(`
@minLength(1)
@maxLength(2)
@pattern("a|b")
@format("ipv4")
@contentEncoding("base64url")
@contentMediaType("application/jwt")
@contentSchema(JwtToken)
union Test {
string, int32, null
}
model JwtToken is Array<Record<string>>;
`);
assertStringConstraints(schemas["Test.json"]);
});
it("on property", async () => {
const schemas = await emitSchema(`
model Test {
@minLength(1)
@maxLength(2)
@pattern("a|b")
@format("ipv4")
@contentEncoding("base64url")
@contentMediaType("application/jwt")
@contentSchema(JwtToken)
prop: string;
}
model JwtToken is Array<Record<string>>;
`);
assertStringConstraints(schemas["Test.json"].properties.prop);
});
});
it("combine with constraint of base scalar", async () => {
const schemas = await emitSchema(`
@minValue(1)
scalar base extends int32;
@maxValue(2)
scalar test extends base;
`);
strictEqual(schemas["test.json"].minimum, 1);
strictEqual(schemas["test.json"].maximum, 2);
});
});