Skip to content

Commit 04f4da2

Browse files
authored
Revision 0.31.17 (#608)
* Fix Compiler Contains Check * Updates * Version
1 parent dc4e70d commit 04f4da2

File tree

6 files changed

+122
-4
lines changed

6 files changed

+122
-4
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@sinclair/typebox",
3-
"version": "0.31.16",
3+
"version": "0.31.17",
44
"description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
55
"keywords": [
66
"typescript",

src/compiler/compiler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ export namespace TypeCompiler {
204204
const checkExpression = CreateExpression(containsSchema, references, 'value')
205205
const checkMinContains = IsNumber(schema.minContains) ? [`(count >= ${schema.minContains})`] : []
206206
const checkMaxContains = IsNumber(schema.maxContains) ? [`(count <= ${schema.maxContains})`] : []
207-
const checkCount = `const count = ${value}.reduce((${accumulator}, ${parameter}) => ${checkExpression} ? acc + 1 : acc, 0)`
207+
const checkCount = `const count = value.reduce((${accumulator}, ${parameter}) => ${checkExpression} ? acc + 1 : acc, 0)`
208208
const check = [`(count > 0)`, ...checkMinContains, ...checkMaxContains].join(' && ')
209209
yield `((${parameter}) => { ${checkCount}; return ${check}})(${value})`
210210
}

test/runtime/compiler-ajv/array.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,42 @@ describe('compiler-ajv/Array', () => {
145145
Fail(T, [1, 1, 1, 1, 1])
146146
Fail(T, [1, 1, 1, 1, 1, 1])
147147
})
148+
// ----------------------------------------------------------------
149+
// Issue: https://github.com/sinclairzx81/typebox/discussions/607
150+
// ----------------------------------------------------------------
151+
it('Should correctly handle undefined array properties', () => {
152+
const Answer = Type.Object({
153+
text: Type.String(),
154+
isCorrect: Type.Boolean(),
155+
})
156+
const Question = Type.Object({
157+
text: Type.String(),
158+
options: Type.Array(Answer, {
159+
minContains: 1,
160+
maxContains: 1,
161+
contains: Type.Object({
162+
text: Type.String(),
163+
isCorrect: Type.Literal(true),
164+
}),
165+
}),
166+
})
167+
Fail(Question, { text: 'A' })
168+
Fail(Question, { text: 'A', options: [] })
169+
Ok(Question, { text: 'A', options: [{ text: 'A', isCorrect: true }] })
170+
Ok(Question, {
171+
text: 'A',
172+
options: [
173+
{ text: 'A', isCorrect: true },
174+
{ text: 'B', isCorrect: false },
175+
],
176+
})
177+
Fail(Question, { text: 'A', options: [{ text: 'A', isCorrect: false }] })
178+
Fail(Question, {
179+
text: 'A',
180+
options: [
181+
{ text: 'A', isCorrect: true },
182+
{ text: 'B', isCorrect: true },
183+
],
184+
})
185+
})
148186
})

test/runtime/compiler/array.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,42 @@ describe('compiler/Array', () => {
145145
Fail(T, [1, 1, 1, 1, 1])
146146
Fail(T, [1, 1, 1, 1, 1, 1])
147147
})
148+
// ----------------------------------------------------------------
149+
// Issue: https://github.com/sinclairzx81/typebox/discussions/607
150+
// ----------------------------------------------------------------
151+
it('Should correctly handle undefined array properties', () => {
152+
const Answer = Type.Object({
153+
text: Type.String(),
154+
isCorrect: Type.Boolean(),
155+
})
156+
const Question = Type.Object({
157+
text: Type.String(),
158+
options: Type.Array(Answer, {
159+
minContains: 1,
160+
maxContains: 1,
161+
contains: Type.Object({
162+
text: Type.String(),
163+
isCorrect: Type.Literal(true),
164+
}),
165+
}),
166+
})
167+
Fail(Question, { text: 'A' })
168+
Fail(Question, { text: 'A', options: [] })
169+
Ok(Question, { text: 'A', options: [{ text: 'A', isCorrect: true }] })
170+
Ok(Question, {
171+
text: 'A',
172+
options: [
173+
{ text: 'A', isCorrect: true },
174+
{ text: 'B', isCorrect: false },
175+
],
176+
})
177+
Fail(Question, { text: 'A', options: [{ text: 'A', isCorrect: false }] })
178+
Fail(Question, {
179+
text: 'A',
180+
options: [
181+
{ text: 'A', isCorrect: true },
182+
{ text: 'B', isCorrect: true },
183+
],
184+
})
185+
})
148186
})

test/runtime/value/check/array.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,46 @@ describe('value/check/Array', () => {
110110
Assert.IsFalse(Value.Check(T, [1, 1, 1, 1, 1]))
111111
Assert.IsFalse(Value.Check(T, [1, 1, 1, 1, 1, 1]))
112112
})
113+
// ----------------------------------------------------------------
114+
// Issue: https://github.com/sinclairzx81/typebox/discussions/607
115+
// ----------------------------------------------------------------
116+
it('Should correctly handle undefined array properties', () => {
117+
const Answer = Type.Object({
118+
text: Type.String(),
119+
isCorrect: Type.Boolean(),
120+
})
121+
const Question = Type.Object({
122+
text: Type.String(),
123+
options: Type.Array(Answer, {
124+
minContains: 1,
125+
maxContains: 1,
126+
contains: Type.Object({
127+
text: Type.String(),
128+
isCorrect: Type.Literal(true),
129+
}),
130+
}),
131+
})
132+
Assert.IsFalse(Value.Check(Question, { text: 'A' }))
133+
Assert.IsFalse(Value.Check(Question, { text: 'A', options: [] }))
134+
Assert.IsTrue(Value.Check(Question, { text: 'A', options: [{ text: 'A', isCorrect: true }] }))
135+
Assert.IsTrue(
136+
Value.Check(Question, {
137+
text: 'A',
138+
options: [
139+
{ text: 'A', isCorrect: true },
140+
{ text: 'B', isCorrect: false },
141+
],
142+
}),
143+
)
144+
Assert.IsFalse(Value.Check(Question, { text: 'A', options: [{ text: 'A', isCorrect: false }] }))
145+
Assert.IsFalse(
146+
Value.Check(Question, {
147+
text: 'A',
148+
options: [
149+
{ text: 'A', isCorrect: true },
150+
{ text: 'B', isCorrect: true },
151+
],
152+
}),
153+
)
154+
})
113155
})

0 commit comments

Comments
 (0)