Skip to content

Commit

Permalink
Revision 0.31.17 (#608)
Browse files Browse the repository at this point in the history
* Fix Compiler Contains Check

* Updates

* Version
  • Loading branch information
sinclairzx81 authored Sep 26, 2023
1 parent dc4e70d commit 04f4da2
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sinclair/typebox",
"version": "0.31.16",
"version": "0.31.17",
"description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
"keywords": [
"typescript",
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ export namespace TypeCompiler {
const checkExpression = CreateExpression(containsSchema, references, 'value')
const checkMinContains = IsNumber(schema.minContains) ? [`(count >= ${schema.minContains})`] : []
const checkMaxContains = IsNumber(schema.maxContains) ? [`(count <= ${schema.maxContains})`] : []
const checkCount = `const count = ${value}.reduce((${accumulator}, ${parameter}) => ${checkExpression} ? acc + 1 : acc, 0)`
const checkCount = `const count = value.reduce((${accumulator}, ${parameter}) => ${checkExpression} ? acc + 1 : acc, 0)`
const check = [`(count > 0)`, ...checkMinContains, ...checkMaxContains].join(' && ')
yield `((${parameter}) => { ${checkCount}; return ${check}})(${value})`
}
Expand Down
38 changes: 38 additions & 0 deletions test/runtime/compiler-ajv/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,42 @@ describe('compiler-ajv/Array', () => {
Fail(T, [1, 1, 1, 1, 1])
Fail(T, [1, 1, 1, 1, 1, 1])
})
// ----------------------------------------------------------------
// Issue: https://github.com/sinclairzx81/typebox/discussions/607
// ----------------------------------------------------------------
it('Should correctly handle undefined array properties', () => {
const Answer = Type.Object({
text: Type.String(),
isCorrect: Type.Boolean(),
})
const Question = Type.Object({
text: Type.String(),
options: Type.Array(Answer, {
minContains: 1,
maxContains: 1,
contains: Type.Object({
text: Type.String(),
isCorrect: Type.Literal(true),
}),
}),
})
Fail(Question, { text: 'A' })
Fail(Question, { text: 'A', options: [] })
Ok(Question, { text: 'A', options: [{ text: 'A', isCorrect: true }] })
Ok(Question, {
text: 'A',
options: [
{ text: 'A', isCorrect: true },
{ text: 'B', isCorrect: false },
],
})
Fail(Question, { text: 'A', options: [{ text: 'A', isCorrect: false }] })
Fail(Question, {
text: 'A',
options: [
{ text: 'A', isCorrect: true },
{ text: 'B', isCorrect: true },
],
})
})
})
38 changes: 38 additions & 0 deletions test/runtime/compiler/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,42 @@ describe('compiler/Array', () => {
Fail(T, [1, 1, 1, 1, 1])
Fail(T, [1, 1, 1, 1, 1, 1])
})
// ----------------------------------------------------------------
// Issue: https://github.com/sinclairzx81/typebox/discussions/607
// ----------------------------------------------------------------
it('Should correctly handle undefined array properties', () => {
const Answer = Type.Object({
text: Type.String(),
isCorrect: Type.Boolean(),
})
const Question = Type.Object({
text: Type.String(),
options: Type.Array(Answer, {
minContains: 1,
maxContains: 1,
contains: Type.Object({
text: Type.String(),
isCorrect: Type.Literal(true),
}),
}),
})
Fail(Question, { text: 'A' })
Fail(Question, { text: 'A', options: [] })
Ok(Question, { text: 'A', options: [{ text: 'A', isCorrect: true }] })
Ok(Question, {
text: 'A',
options: [
{ text: 'A', isCorrect: true },
{ text: 'B', isCorrect: false },
],
})
Fail(Question, { text: 'A', options: [{ text: 'A', isCorrect: false }] })
Fail(Question, {
text: 'A',
options: [
{ text: 'A', isCorrect: true },
{ text: 'B', isCorrect: true },
],
})
})
})
42 changes: 42 additions & 0 deletions test/runtime/value/check/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,46 @@ describe('value/check/Array', () => {
Assert.IsFalse(Value.Check(T, [1, 1, 1, 1, 1]))
Assert.IsFalse(Value.Check(T, [1, 1, 1, 1, 1, 1]))
})
// ----------------------------------------------------------------
// Issue: https://github.com/sinclairzx81/typebox/discussions/607
// ----------------------------------------------------------------
it('Should correctly handle undefined array properties', () => {
const Answer = Type.Object({
text: Type.String(),
isCorrect: Type.Boolean(),
})
const Question = Type.Object({
text: Type.String(),
options: Type.Array(Answer, {
minContains: 1,
maxContains: 1,
contains: Type.Object({
text: Type.String(),
isCorrect: Type.Literal(true),
}),
}),
})
Assert.IsFalse(Value.Check(Question, { text: 'A' }))
Assert.IsFalse(Value.Check(Question, { text: 'A', options: [] }))
Assert.IsTrue(Value.Check(Question, { text: 'A', options: [{ text: 'A', isCorrect: true }] }))
Assert.IsTrue(
Value.Check(Question, {
text: 'A',
options: [
{ text: 'A', isCorrect: true },
{ text: 'B', isCorrect: false },
],
}),
)
Assert.IsFalse(Value.Check(Question, { text: 'A', options: [{ text: 'A', isCorrect: false }] }))
Assert.IsFalse(
Value.Check(Question, {
text: 'A',
options: [
{ text: 'A', isCorrect: true },
{ text: 'B', isCorrect: true },
],
}),
)
})
})

0 comments on commit 04f4da2

Please sign in to comment.