Skip to content

Commit

Permalink
Revision 0.32.24 (#848)
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed Apr 25, 2024
1 parent 2a2b5d9 commit cfc152b
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 13 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
@@ -1,6 +1,6 @@
{
"name": "@sinclair/typebox",
"version": "0.32.23",
"version": "0.32.24",
"description": "Json Schema Type Builder with Static Type Resolution for TypeScript",
"keywords": [
"typescript",
Expand Down
14 changes: 8 additions & 6 deletions src/value/convert/convert.ts
Expand Up @@ -54,7 +54,7 @@ import type { TUndefined } from '../../type/undefined/index'
// ------------------------------------------------------------------
// ValueGuard
// ------------------------------------------------------------------
import { IsArray, IsObject, IsDate, IsUndefined, IsString, IsNumber, IsBoolean, IsBigInt, IsSymbol } from '../guard/index'
import { IsArray, IsObject, IsDate, IsUndefined, IsString, IsNumber, IsBoolean, IsBigInt, IsSymbol, HasPropertyKey } from '../guard/index'
// ------------------------------------------------------------------
// Conversions
// ------------------------------------------------------------------
Expand Down Expand Up @@ -194,11 +194,13 @@ function FromNumber(schema: TNumber, references: TSchema[], value: any): unknown
function FromObject(schema: TObject, references: TSchema[], value: any): unknown {
const isConvertable = IsObject(value)
if(!isConvertable) return value
return Object.getOwnPropertyNames(schema.properties).reduce((value, key) => {
return !IsUndefined(value[key])
? ({ ...value, [key]: Visit(schema.properties[key], references, value[key]) })
: ({ ...value })
}, value)
const result: Record<PropertyKey, unknown> = {}
for(const key of Object.keys(value)) {
result[key] = HasPropertyKey(schema.properties, key)
? Visit(schema.properties[key], references, value[key])
: value[key]
}
return result
}
function FromRecord(schema: TRecord, references: TSchema[], value: any): unknown {
const propertyKey = Object.getOwnPropertyNames(schema.patternProperties)[0]
Expand Down
6 changes: 3 additions & 3 deletions src/value/value/value.ts
Expand Up @@ -75,11 +75,11 @@ export function Clean(schema: TSchema, value: unknown): unknown
export function Clean(...args: any[]) {
return CleanValue.apply(CleanValue, args as any)
}
/** Converts any type mismatched values to their target type if a reasonable conversion is possible */
/** Converts any type mismatched values to their target type if a reasonable conversion is possible. */
export function Convert(schema: TSchema, references: TSchema[], value: unknown): unknown
/** Converts any type mismatched values to their target type if a reasonable conversion is possibl. */
/** Converts any type mismatched values to their target type if a reasonable conversion is possible. */
export function Convert(schema: TSchema, value: unknown): unknown
/** Converts any type mismatched values to their target type if a reasonable conversion is possible */
/** Converts any type mismatched values to their target type if a reasonable conversion is possible. */
export function Convert(...args: any[]) {
return ConvertValue.apply(ConvertValue, args as any)
}
Expand Down
10 changes: 9 additions & 1 deletion test/runtime/value/convert/object.ts
Expand Up @@ -2,9 +2,9 @@ import { Value } from '@sinclair/typebox/value'
import { Type } from '@sinclair/typebox'
import { Assert } from '../../assert/index'

// prettier-ignore
describe('value/convert/Object', () => {
it('Should convert properties', () => {
// prettier-ignore
const T = Type.Object({
x: Type.Number(),
y: Type.Boolean(),
Expand All @@ -13,4 +13,12 @@ describe('value/convert/Object', () => {
const R = Value.Convert(T, { x: '42', y: 'true', z: 'hello' })
Assert.IsEqual(R, { x: 42, y: true, z: 'hello' })
})
it('Should convert known properties', () => {
const T = Type.Object({
x: Type.Number(),
y: Type.Boolean()
})
const R = Value.Convert(T, { x: '42', y: 'true', z: 'hello' })
Assert.IsEqual(R, { x: 42, y: true, z: 'hello' })
})
})

0 comments on commit cfc152b

Please sign in to comment.