Additional Defaults (#294)

This commit is contained in:
sinclairzx81
2022-12-19 22:50:32 +09:00
committed by GitHub
parent 1cb07ad032
commit 0ce824bfb3
7 changed files with 40 additions and 7 deletions
+1 -1
View File
@@ -10,7 +10,7 @@ import { Type, Kind, Static, TSchema } from '@sinclair/typebox'
const T = Type.Object({
x: Type.Number(),
y: Type.Number(),
z: Type.Number()
z: Type.Number(),
})
type T = Static<typeof T>
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@sinclair/typebox",
"version": "0.25.13",
"version": "0.25.14",
"description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
"keywords": [
"typescript",
+20 -4
View File
@@ -121,7 +121,11 @@ export namespace ValueCreate {
}
function Literal(schema: Types.TLiteral, references: Types.TSchema[]): any {
return schema.const
if (schema.default !== undefined) {
return schema.default
} else {
return schema.const
}
}
function Never(schema: Types.TNever, references: Types.TSchema[]): any {
@@ -129,7 +133,11 @@ export namespace ValueCreate {
}
function Null(schema: Types.TNull, references: Types.TSchema[]): any {
return null
if (schema.default !== undefined) {
return schema.default
} else {
return null
}
}
function Number(schema: Types.TNumber, references: Types.TSchema[]): any {
@@ -244,7 +252,11 @@ export namespace ValueCreate {
}
function Undefined(schema: Types.TUndefined, references: Types.TSchema[]): any {
return undefined
if (schema.default !== undefined) {
return schema.default
} else {
return undefined
}
}
function Union(schema: Types.TUnion<any[]>, references: Types.TSchema[]): any {
@@ -275,7 +287,11 @@ export namespace ValueCreate {
}
function Void(schema: Types.TVoid, references: Types.TSchema[]): any {
return null
if (schema.default !== undefined) {
return schema.default
} else {
return null
}
}
function UserDefined(schema: Types.TSchema, references: Types.TSchema[]): any {
+5 -1
View File
@@ -2,7 +2,7 @@ import { Value } from '@sinclair/typebox/value'
import { Type } from '@sinclair/typebox'
import { Assert } from '../../assert/index'
describe('value/create/KeyOf', () => {
describe('value/create/Literal', () => {
it('Should create literal string', () => {
const T = Type.Literal('hello')
Assert.deepEqual(Value.Create(T), 'hello')
@@ -15,4 +15,8 @@ describe('value/create/KeyOf', () => {
const T = Type.Literal(true)
Assert.deepEqual(Value.Create(T), true)
})
it('Should create literal from default value', () => {
const T = Type.Literal(true, { default: 'hello' })
Assert.deepEqual(Value.Create(T), 'hello')
})
})
+4
View File
@@ -7,4 +7,8 @@ describe('value/create/Null', () => {
const T = Type.Null()
Assert.deepEqual(Value.Create(T), null)
})
it('Should create null from default value', () => {
const T = Type.Null({ default: 'hello' })
Assert.deepEqual(Value.Create(T), 'hello')
})
})
+4
View File
@@ -7,4 +7,8 @@ describe('value/create/Undefined', () => {
const T = Type.Undefined()
Assert.deepEqual(Value.Create(T), undefined)
})
it('Should create value from default value', () => {
const T = Type.Undefined({ default: 'hello' })
Assert.deepEqual(Value.Create(T), 'hello')
})
})
+5
View File
@@ -7,4 +7,9 @@ describe('value/create/Void', () => {
const T = Type.Void()
Assert.deepEqual(Value.Create(T), null)
})
it('Should create value from default value', () => {
const T = Type.Void({ default: 'hello' })
Assert.deepEqual(Value.Create(T), 'hello')
})
})