Skip to content

Commit

Permalink
Revision 0.31.13 (#583)
Browse files Browse the repository at this point in the history
* Support Intersect Value Conversion

* Update Test To Include Non Converted Property
  • Loading branch information
sinclairzx81 authored Sep 5, 2023
1 parent 4211f4a commit 4edc46c
Show file tree
Hide file tree
Showing 5 changed files with 57 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.12",
"version": "0.31.13",
"description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
"keywords": [
"typescript",
Expand Down
9 changes: 8 additions & 1 deletion src/value/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ function TDate(schema: Types.TDate, references: Types.TSchema[], value: any): un
function TInteger(schema: Types.TInteger, references: Types.TSchema[], value: any): unknown {
return TryConvertInteger(value)
}
function TIntersect(schema: Types.TIntersect, references: Types.TSchema[], value: any): unknown {
// prettier-ignore
return (schema.allOf.every(schema => Types.TypeGuard.TObject(schema)))
? Visit(Types.Type.Composite(schema.allOf as Types.TObject[]), references, value)
: Visit(schema.allOf[0], references, value)
}
function TLiteral(schema: Types.TLiteral, references: Types.TSchema[], value: any): unknown {
return TryConvertLiteral(schema, value)
}
Expand Down Expand Up @@ -247,6 +253,8 @@ function Visit(schema: Types.TSchema, references: Types.TSchema[], value: any):
return TDate(schema_, references_, value)
case 'Integer':
return TInteger(schema_, references_, value)
case 'Intersect':
return TIntersect(schema_, references_, value)
case 'Literal':
return TLiteral(schema_, references_, value)
case 'Null':
Expand Down Expand Up @@ -278,7 +286,6 @@ function Visit(schema: Types.TSchema, references: Types.TSchema[], value: any):
case 'AsyncIterator':
case 'Constructor':
case 'Function':
case 'Intersect':
case 'Iterator':
case 'Never':
case 'Promise':
Expand Down
1 change: 1 addition & 0 deletions test/runtime/value/convert/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import './date'
import './enum'
import './function'
import './integer'
import './intersect'
import './iterator'
import './keyof'
import './literal'
Expand Down
45 changes: 45 additions & 0 deletions test/runtime/value/convert/intersect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Value } from '@sinclair/typebox/value'
import { Type } from '@sinclair/typebox'
import { Assert } from '../../assert/index'

describe('value/convert/Intersect', () => {
it('Should convert intersected objects', () => {
// prettier-ignore
const T = Type.Intersect([
Type.Object({ x: Type.Number() }),
Type.Object({ y: Type.Number() })
])
const R = Value.Convert(T, { x: '1', y: '2' })
Assert.IsEqual(R, { x: 1, y: 2 })
})
it('Should not convert for non object exclusive intersect', () => {
// prettier-ignore
const T = Type.Intersect([
Type.Number(),
Type.Object({ x: Type.Number() }),
Type.Object({ y: Type.Number() })
])
const R = Value.Convert(T, { x: '1', y: '2' })
Assert.IsEqual(R, { x: '1', y: '2' })
})
it('Should convert first type for object exclusive intersect 1', () => {
// prettier-ignore
const T = Type.Intersect([
Type.Number(),
Type.Object({ x: Type.Number() }),
Type.Object({ y: Type.Number() })
])
const R = Value.Convert(T, '123')
Assert.IsEqual(R, 123)
})
it('Should convert first type for object exclusive intersect 2', () => {
// prettier-ignore
const T = Type.Intersect([
Type.Object({ x: Type.Number() }),
Type.Object({ y: Type.Number() }),
Type.Number(),
])
const R = Value.Convert(T, { x: '3', y: '4' })
Assert.IsEqual(R, { x: 3, y: '4' })
})
})

0 comments on commit 4edc46c

Please sign in to comment.