Confusion with ArrayOptions.contains
#607
-
I've two use cases here:
I've two Schemas one for a Question and one for Option. What would be the correct way to achieve this? const OptionsDTO = t.Object({
text: t.String(),
isCorrect: t.Boolean()
})
const QuestionDTO = t.Object({
text: t.String(),
options: t.Array(OptionsDTO, {
minItems: 2,
maxItems: 5,
contains: t.Object({
text: t.String(),
isCorrect: t.Literal(true)
})
})
}) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
@cheersmas Heya, have you tried import { Type } from '@sinclair/typebox'
import { Value } from '@sinclair/typebox/value'
const Answer = Type.Object({
text: Type.String(),
isCorrect: Type.Boolean()
})
const SingleChoice = Type.Object({
text: Type.String(),
options: Type.Array(Answer, {
minContains: 1, // expect ONLY 1 element to contain schematic
maxContains: 1,
contains: Type.Object({
text: Type.String(),
isCorrect: Type.Literal(true)
})
})
})
const MultiChoice = Type.Object({
text: Type.String(),
options: Type.Array(Answer, {
minContains: 1, // expect at least 1 element to contain schematic
contains: Type.Object({
text: Type.String(),
isCorrect: Type.Literal(true)
})
})
})
// ------------------------------------------------------------------
// SingleChoice
// ------------------------------------------------------------------
const A = Value.Check(SingleChoice, { // true
text: 'SingleChoice',
options: [
{ text: 'A', isCorrect: true },
{ text: 'B', isCorrect: false }
]
})
const B = Value.Check(SingleChoice, { // false
text: 'SingleChoice',
options: [
{ text: 'A', isCorrect: true },
{ text: 'B', isCorrect: true }
]
})
// ------------------------------------------------------------------
// MultiChoice
// ------------------------------------------------------------------
const C = Value.Check(MultiChoice, { // true
text: 'MultiChoice',
options: [
{ text: 'A', isCorrect: true },
{ text: 'B', isCorrect: true }
]
})
const D = Value.Check(MultiChoice, { // false
text: 'MultiChoice',
options: [
{ text: 'A', isCorrect: false },
{ text: 'B', isCorrect: false }
]
}) |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
@cheersmas Hi. I have just published a fix for this which should prevent errors being thrown (this was a good catch)
The update is on
@sinclair/typebox@0.31.17
so you will need to ensure Elysia is on this version. Associated PR can be found #608.