Skip to content

Commit

Permalink
Revision 0.33.9 (#984)
Browse files Browse the repository at this point in the history
* Generate Interior Intersect Errors

* Version

* ChangeLog
  • Loading branch information
sinclairzx81 authored Sep 2, 2024
1 parent ed4c89a commit d387b2a
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 15 deletions.
3 changes: 2 additions & 1 deletion changelog/0.33.0.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### 0.33.0

- [Revision 0.33.9](https://github.com/sinclairzx81/typebox/pull/984)
- [887](https://github.com/sinclairzx81/typebox/issues/887) Generate Nested Intersect Errors
- [Revision 0.33.8](https://github.com/sinclairzx81/typebox/pull/983)
- [982](https://github.com/sinclairzx81/typebox/issues/982) Prevent Intersect Transform Encode callback from being called twice
- [974](https://github.com/sinclairzx81/typebox/issues/974) Make strict the Encode and Decode return type
Expand Down
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.33.8",
"version": "0.33.9",
"description": "Json Schema Type Builder with Static Type Resolution for TypeScript",
"keywords": [
"typescript",
Expand Down
11 changes: 7 additions & 4 deletions src/errors/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,13 +312,16 @@ function* FromInteger(schema: TInteger, references: TSchema[], path: string, val
}
}
function* FromIntersect(schema: TIntersect, references: TSchema[], path: string, value: any): IterableIterator<ValueError> {
let hasError = false
for (const inner of schema.allOf) {
const next = Visit(inner, references, path, value).next()
if (!next.done) {
yield Create(ValueErrorType.Intersect, schema, path, value)
yield next.value
for (const error of Visit(inner, references, path, value)) {
hasError = true
yield error
}
}
if (hasError) {
return yield Create(ValueErrorType.Intersect, schema, path, value)
}
if (schema.unevaluatedProperties === false) {
const keyCheck = new RegExp(KeyOfPattern(schema))
for (const valueKey of Object.getOwnPropertyNames(value)) {
Expand Down
39 changes: 32 additions & 7 deletions test/runtime/errors/types/intersect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,46 @@ import { Resolve } from './resolve'
import { Assert } from '../../assert'

describe('errors/type/Intersect', () => {
const T = Type.Intersect([Type.Object({ x: Type.Number() }), Type.Object({ y: Type.Number() })])
it('Should pass 0', () => {
const T = Type.Intersect([Type.Object({ x: Type.Number() }), Type.Object({ y: Type.Number() })])
const R = Resolve(T, { x: 1, y: 1 })
Assert.IsEqual(R.length, 0)
})
it('Should pass 1', () => {
const T = Type.Intersect([Type.Object({ x: Type.Number() }), Type.Object({ y: Type.Number() })])
const R = Resolve(T, { x: 1 })
Assert.IsEqual(R.length, 2)
Assert.IsEqual(R[0].type, ValueErrorType.Intersect)
Assert.IsEqual(R[1].type, ValueErrorType.ObjectRequiredProperty)
Assert.IsEqual(R.length, 3)
Assert.IsEqual(R[0].type, ValueErrorType.ObjectRequiredProperty)
Assert.IsEqual(R[1].type, ValueErrorType.Number)
Assert.IsEqual(R[2].type, ValueErrorType.Intersect)
})
it('Should pass 2', () => {
const T = Type.Intersect([Type.Object({ x: Type.Number() }), Type.Object({ y: Type.Number() })])
const R = Resolve(T, { y: 1 })
Assert.IsEqual(R.length, 2)
Assert.IsEqual(R[0].type, ValueErrorType.Intersect)
Assert.IsEqual(R[1].type, ValueErrorType.ObjectRequiredProperty)
Assert.IsEqual(R.length, 3)
Assert.IsEqual(R[0].type, ValueErrorType.ObjectRequiredProperty)
Assert.IsEqual(R[1].type, ValueErrorType.Number)
Assert.IsEqual(R[2].type, ValueErrorType.Intersect)
})
// ----------------------------------------------------------------
// https://github.com/sinclairzx81/typebox/issues/887
// ----------------------------------------------------------------
it('Should pass 3', () => {
const A = Type.Intersect([Type.Object({ x: Type.Number() }), Type.Object({ y: Type.Number() })])
const B = Type.Intersect([Type.Object({ x: Type.Number() }), Type.Object({ z: Type.Number() })])
const T = Type.Intersect([A, B])
const R = Resolve(T, {})
Assert.IsEqual(R.length, 11)
Assert.IsEqual(R[0].type, ValueErrorType.ObjectRequiredProperty)
Assert.IsEqual(R[1].type, ValueErrorType.Number)
Assert.IsEqual(R[2].type, ValueErrorType.ObjectRequiredProperty)
Assert.IsEqual(R[3].type, ValueErrorType.Number)
Assert.IsEqual(R[4].type, ValueErrorType.Intersect)
Assert.IsEqual(R[5].type, ValueErrorType.ObjectRequiredProperty)
Assert.IsEqual(R[6].type, ValueErrorType.Number)
Assert.IsEqual(R[7].type, ValueErrorType.ObjectRequiredProperty)
Assert.IsEqual(R[8].type, ValueErrorType.Number)
Assert.IsEqual(R[9].type, ValueErrorType.Intersect)
Assert.IsEqual(R[10].type, ValueErrorType.Intersect)
})
})

0 comments on commit d387b2a

Please sign in to comment.