diff --git a/src/value/default/default.ts b/src/value/default/default.ts new file mode 100644 index 000000000..de84d55e8 --- /dev/null +++ b/src/value/default/default.ts @@ -0,0 +1,179 @@ +/*-------------------------------------------------------------------------- + +@sinclair/typebox/value + +The MIT License (MIT) + +Copyright (c) 2017-2023 Haydn Paterson (sinclair) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + +---------------------------------------------------------------------------*/ + +import { IsString, IsObject, IsArray, IsUndefined } from '../guard/index' +import { TSchema as IsSchemaType } from '../../type/guard/type' +import { Check } from '../check/index' +import { Deref } from '../deref/index' +import { Kind } from '../../type/symbols/index' + +import type { TSchema } from '../../type/schema/index' +import type { TArray } from '../../type/array/index' +import type { TIntersect } from '../../type/intersect/index' +import type { TObject } from '../../type/object/index' +import type { TRecord } from '../../type/record/index' +import type { TRef } from '../../type/ref/index' +import type { TThis } from '../../type/recursive/index' +import type { TTuple } from '../../type/tuple/index' +import type { TUnion } from '../../type/union/index' + +// ------------------------------------------------------------------ +// ValueOrDefault +// ------------------------------------------------------------------ +function ValueOrDefault(schema: TSchema, value: unknown) { + return !(value === undefined) || !('default' in schema) ? value : schema.default +} +// ------------------------------------------------------------------ +// IsCheckable +// ------------------------------------------------------------------ +function IsCheckable(schema: unknown): boolean { + return IsSchemaType(schema) && schema[Kind] !== 'Unsafe' +} +// ------------------------------------------------------------------ +// IsDefaultSchema +// ------------------------------------------------------------------ +function IsDefaultSchema(value: unknown): value is TSchema { + return IsSchemaType(value) && 'default' in value +} +// ------------------------------------------------------------------ +// Types +// ------------------------------------------------------------------ +function TArray(schema: TArray, references: TSchema[], value: unknown): any { + const defaulted = ValueOrDefault(schema, value) + if (!IsArray(defaulted)) return defaulted + for (let i = 0; i < defaulted.length; i++) { + defaulted[i] = Visit(schema.items, references, defaulted[i]) + } + return defaulted +} +function TIntersect(schema: TIntersect, references: TSchema[], value: unknown): any { + const defaulted = ValueOrDefault(schema, value) + return schema.allOf.reduce((acc, schema) => { + const next = Visit(schema, references, defaulted) + return IsObject(next) ? { ...acc, ...next } : next + }, {}) +} +function TObject(schema: TObject, references: TSchema[], value: unknown): any { + const defaulted = ValueOrDefault(schema, value) + if (!IsObject(defaulted)) return defaulted + const additionalPropertiesSchema = schema.additionalProperties as TSchema + const knownPropertyKeys = Object.getOwnPropertyNames(schema.properties) + // properties + for (const key of knownPropertyKeys) { + if (!IsDefaultSchema(schema.properties[key])) continue + defaulted[key] = Visit(schema.properties[key], references, defaulted[key]) + } + // return if not additional properties + if (!IsDefaultSchema(additionalPropertiesSchema)) return defaulted + // additional properties + for (const key of Object.getOwnPropertyNames(defaulted)) { + if (knownPropertyKeys.includes(key)) continue + defaulted[key] = Visit(additionalPropertiesSchema, references, defaulted[key]) + } + return defaulted +} +function TRecord(schema: TRecord, references: TSchema[], value: unknown): any { + const defaulted = ValueOrDefault(schema, value) + if (!IsObject(defaulted)) return defaulted + const additionalPropertiesSchema = schema.additionalProperties as TSchema + const [propertyKeyPattern, propertySchema] = Object.entries(schema.patternProperties)[0] + const knownPropertyKey = new RegExp(propertyKeyPattern) + // properties + for (const key of Object.getOwnPropertyNames(defaulted)) { + if (!(knownPropertyKey.test(key) && IsDefaultSchema(propertySchema))) continue + defaulted[key] = Visit(propertySchema, references, defaulted[key]) + } + // return if not additional properties + if (!IsDefaultSchema(additionalPropertiesSchema)) return defaulted + // additional properties + for (const key of Object.getOwnPropertyNames(defaulted)) { + if (knownPropertyKey.test(key)) continue + defaulted[key] = Visit(additionalPropertiesSchema, references, defaulted[key]) + } + return defaulted +} +function TRef(schema: TRef, references: TSchema[], value: unknown): any { + return Visit(Deref(schema, references), references, ValueOrDefault(schema, value)) +} +function TThis(schema: TThis, references: TSchema[], value: unknown): any { + return Visit(Deref(schema, references), references, value) +} +function TTuple(schema: TTuple, references: TSchema[], value: unknown): any { + const defaulted = ValueOrDefault(schema, value) + if (!IsArray(defaulted) || IsUndefined(schema.items)) return defaulted + const [items, max] = [schema.items!, Math.max(schema.items!.length, defaulted.length)] + for (let i = 0; i < max; i++) { + if (i < items.length) defaulted[i] = Visit(items[i], references, defaulted[i]) + } + return defaulted +} +function TUnion(schema: TUnion, references: TSchema[], value: unknown): any { + const defaulted = ValueOrDefault(schema, value) + for (const inner of schema.anyOf) { + const result = Visit(inner, references, defaulted) + if (IsCheckable(inner) && Check(inner, result)) { + return result + } + } + return defaulted +} +function Visit(schema: TSchema, references: TSchema[], value: unknown): any { + const references_ = IsString(schema.$id) ? [...references, schema] : references + const schema_ = schema as any + switch (schema_[Kind]) { + case 'Array': + return TArray(schema_, references_, value) + case 'Intersect': + return TIntersect(schema_, references_, value) + case 'Object': + return TObject(schema_, references_, value) + case 'Record': + return TRecord(schema_, references_, value) + case 'Ref': + return TRef(schema_, references_, value) + case 'This': + return TThis(schema_, references_, value) + case 'Tuple': + return TTuple(schema_, references_, value) + case 'Union': + return TUnion(schema_, references_, value) + default: + return ValueOrDefault(schema_, value) + } +} +// ------------------------------------------------------------------ +// Default +// ------------------------------------------------------------------ +/** Applies default annotations to missing values and returns the result. This function does not type check the input value and may return invalid results for invalid inputs. You should type check the result before use. */ +export function Default(schema: T, references: TSchema[], value: unknown): unknown +/** Applies default annotations to missing values and returns the result. This function does not type check the input value and may return invalid results for invalid inputs. You should type check the result before use. */ +export function Default(schema: T, value: unknown): unknown +/** Applies default annotations to missing values and returns the result. This function does not type check the input value and may return invalid results for invalid inputs. You should type check the result before use. */ +export function Default(...args: any[]) { + return args.length === 3 ? Visit(args[0], args[1], args[2]) : Visit(args[0], [], args[1]) +} diff --git a/src/value/default/index.ts b/src/value/default/index.ts new file mode 100644 index 000000000..fb74557fd --- /dev/null +++ b/src/value/default/index.ts @@ -0,0 +1,29 @@ +/*-------------------------------------------------------------------------- + +@sinclair/typebox/value + +The MIT License (MIT) + +Copyright (c) 2017-2023 Haydn Paterson (sinclair) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + +---------------------------------------------------------------------------*/ + +export * from './default' diff --git a/src/value/index.ts b/src/value/index.ts index 39b18a832..e238da5f1 100644 --- a/src/value/index.ts +++ b/src/value/index.ts @@ -38,6 +38,7 @@ export { Check } from './check/index' export { Clone } from './clone/index' export { Convert, ValueConvertUnknownTypeError } from './convert/index' export { Create, ValueCreateIntersectTypeError, ValueCreateNeverTypeError, ValueCreateNotTypeError, ValueCreateRecursiveInstantiationError, ValueCreateTempateLiteralTypeError, ValueCreateUnknownTypeError } from './create/index' +export { Default } from './default/index' export { Diff, Patch, Edit, Delete, Insert, Update, ValueDeltaObjectWithSymbolKeyError, ValueDeltaUnableToDiffUnknownValue } from './delta/index' export { Equal } from './equal/index' export { Hash, ValueHashError } from './hash/index' diff --git a/src/value/value/value.ts b/src/value/value/value.ts index 14062af5f..58797c6e3 100644 --- a/src/value/value/value.ts +++ b/src/value/value/value.ts @@ -35,6 +35,7 @@ import { Clone as CloneValue } from '../clone/index' import { Convert as ConvertValue } from '../convert/index' import { Create as CreateValue } from '../create/index' import { Check as CheckValue } from '../check/index' +import { Default as DefaultValue } from '../default/index' import { Diff as DiffValue, Patch as PatchValue, Edit } from '../delta/index' import { Errors as ValueErrors, ValueErrorIterator } from '../../errors/index' @@ -87,6 +88,14 @@ export function Decode(...args: any[]) { if (!Check(schema, references, value)) throw new TransformDecodeCheckError(schema, value, Errors(schema, references, value).First()!) return DecodeValue(schema, references, value) } +/** Applies default annotations to missing values and returns the result. This function does not type check the input value and may return invalid results for invalid inputs. You should type check the result before use. */ +export function Default(schema: T, references: TSchema[], value: unknown): unknown +/** Applies default annotations to missing values and returns the result. This function does not type check the input value and may return invalid results for invalid inputs. You should type check the result before use. */ +export function Default(schema: T, value: unknown): unknown +/** Applies default annotations to missing values and returns the result. This function does not type check the input value and may return invalid results for invalid inputs. You should type check the result before use. */ +export function Default(...args: any[]) { + return DefaultValue.apply(DefaultValue, args as any) +} /** Encodes a value or throws if error */ export function Encode>(schema: T, references: TSchema[], value: unknown): R /** Encodes a value or throws if error */ diff --git a/test/runtime/value/default/any.ts b/test/runtime/value/default/any.ts new file mode 100644 index 000000000..15b12e979 --- /dev/null +++ b/test/runtime/value/default/any.ts @@ -0,0 +1,16 @@ +import { Value } from '@sinclair/typebox/value' +import { Type } from '@sinclair/typebox' +import { Assert } from '../../assert/index' + +describe('value/default/Any', () => { + it('Should use default', () => { + const T = Type.Any({ default: 1 }) + const R = Value.Default(T, undefined) + Assert.IsEqual(R, 1) + }) + it('Should use value', () => { + const T = Type.Any({ default: 1 }) + const R = Value.Default(T, null) + Assert.IsEqual(R, null) + }) +}) diff --git a/test/runtime/value/default/array.ts b/test/runtime/value/default/array.ts new file mode 100644 index 000000000..be385e603 --- /dev/null +++ b/test/runtime/value/default/array.ts @@ -0,0 +1,24 @@ +import { Value } from '@sinclair/typebox/value' +import { Type } from '@sinclair/typebox' +import { Assert } from '../../assert/index' + +describe('value/default/Array', () => { + it('Should use default', () => { + const T = Type.Array(Type.Number(), { default: 1 }) + const R = Value.Default(T, undefined) + Assert.IsEqual(R, 1) + }) + it('Should use value', () => { + const T = Type.Array(Type.Number(), { default: 1 }) + const R = Value.Default(T, null) + Assert.IsEqual(R, null) + }) + // ---------------------------------------------------------------- + // Elements + // ---------------------------------------------------------------- + it('Should use default on elements', () => { + const T = Type.Array(Type.Number({ default: 2 })) + const R = Value.Default(T, [1, undefined, 3]) + Assert.IsEqual(R, [1, 2, 3]) + }) +}) diff --git a/test/runtime/value/default/async-iterator.ts b/test/runtime/value/default/async-iterator.ts new file mode 100644 index 000000000..dd26f579c --- /dev/null +++ b/test/runtime/value/default/async-iterator.ts @@ -0,0 +1,16 @@ +import { Value } from '@sinclair/typebox/value' +import { Type } from '@sinclair/typebox' +import { Assert } from '../../assert/index' + +describe('value/default/AsyncIterator', () => { + it('Should use default', () => { + const T = Type.AsyncIterator(Type.Number(), { default: 1 }) + const R = Value.Default(T, undefined) + Assert.IsEqual(R, 1) + }) + it('Should use value', () => { + const T = Type.AsyncIterator(Type.Number(), { default: 1 }) + const R = Value.Default(T, null) + Assert.IsEqual(R, null) + }) +}) diff --git a/test/runtime/value/default/bigint.ts b/test/runtime/value/default/bigint.ts new file mode 100644 index 000000000..b7e8aa710 --- /dev/null +++ b/test/runtime/value/default/bigint.ts @@ -0,0 +1,16 @@ +import { Value } from '@sinclair/typebox/value' +import { Type } from '@sinclair/typebox' +import { Assert } from '../../assert/index' + +describe('value/default/BigInt', () => { + it('Should use default', () => { + const T = Type.BigInt({ default: 1 }) + const R = Value.Default(T, undefined) + Assert.IsEqual(R, 1) + }) + it('Should use value', () => { + const T = Type.BigInt({ default: 1 }) + const R = Value.Default(T, null) + Assert.IsEqual(R, null) + }) +}) diff --git a/test/runtime/value/default/boolean.ts b/test/runtime/value/default/boolean.ts new file mode 100644 index 000000000..ee05bb06b --- /dev/null +++ b/test/runtime/value/default/boolean.ts @@ -0,0 +1,16 @@ +import { Value } from '@sinclair/typebox/value' +import { Type } from '@sinclair/typebox' +import { Assert } from '../../assert/index' + +describe('value/default/Boolean', () => { + it('Should use default', () => { + const T = Type.Boolean({ default: 1 }) + const R = Value.Default(T, undefined) + Assert.IsEqual(R, 1) + }) + it('Should use value', () => { + const T = Type.Boolean({ default: 1 }) + const R = Value.Default(T, null) + Assert.IsEqual(R, null) + }) +}) diff --git a/test/runtime/value/default/composite.ts b/test/runtime/value/default/composite.ts new file mode 100644 index 000000000..8a73e4106 --- /dev/null +++ b/test/runtime/value/default/composite.ts @@ -0,0 +1,16 @@ +import { Value } from '@sinclair/typebox/value' +import { Type } from '@sinclair/typebox' +import { Assert } from '../../assert/index' + +describe('value/default/Composite', () => { + it('Should use default', () => { + const T = Type.Composite([Type.Object({ x: Type.Number() }), Type.Object({ y: Type.Number() })], { default: 1 }) + const R = Value.Default(T, undefined) + Assert.IsEqual(R, 1) + }) + it('Should use value', () => { + const T = Type.Composite([Type.Object({ x: Type.Number() }), Type.Object({ y: Type.Number() })], { default: 1 }) + const R = Value.Default(T, null) + Assert.IsEqual(R, null) + }) +}) diff --git a/test/runtime/value/default/constructor.ts b/test/runtime/value/default/constructor.ts new file mode 100644 index 000000000..76cb2343f --- /dev/null +++ b/test/runtime/value/default/constructor.ts @@ -0,0 +1,16 @@ +import { Value } from '@sinclair/typebox/value' +import { Type } from '@sinclair/typebox' +import { Assert } from '../../assert/index' + +describe('value/default/Constructor', () => { + it('Should use default', () => { + const T = Type.Constructor([], Type.Number(), { default: 1 }) + const R = Value.Default(T, undefined) + Assert.IsEqual(R, 1) + }) + it('Should use value', () => { + const T = Type.Constructor([], Type.Number(), { default: 1 }) + const R = Value.Default(T, null) + Assert.IsEqual(R, null) + }) +}) diff --git a/test/runtime/value/default/date.ts b/test/runtime/value/default/date.ts new file mode 100644 index 000000000..2522cf20d --- /dev/null +++ b/test/runtime/value/default/date.ts @@ -0,0 +1,16 @@ +import { Value } from '@sinclair/typebox/value' +import { Type } from '@sinclair/typebox' +import { Assert } from '../../assert/index' + +describe('value/default/Date', () => { + it('Should use default', () => { + const T = Type.Date({ default: 1 }) + const R = Value.Default(T, undefined) + Assert.IsEqual(R, 1) + }) + it('Should use value', () => { + const T = Type.Date({ default: 1 }) + const R = Value.Default(T, null) + Assert.IsEqual(R, null) + }) +}) diff --git a/test/runtime/value/default/enum.ts b/test/runtime/value/default/enum.ts new file mode 100644 index 000000000..ae6f9ece2 --- /dev/null +++ b/test/runtime/value/default/enum.ts @@ -0,0 +1,16 @@ +import { Value } from '@sinclair/typebox/value' +import { Type } from '@sinclair/typebox' +import { Assert } from '../../assert/index' + +describe('value/default/Enum', () => { + it('Should use default', () => { + const T = Type.Enum({}, { default: 1 }) + const R = Value.Default(T, undefined) + Assert.IsEqual(R, 1) + }) + it('Should use value', () => { + const T = Type.Enum({}, { default: 1 }) + const R = Value.Default(T, null) + Assert.IsEqual(R, null) + }) +}) diff --git a/test/runtime/value/default/function.ts b/test/runtime/value/default/function.ts new file mode 100644 index 000000000..959c28284 --- /dev/null +++ b/test/runtime/value/default/function.ts @@ -0,0 +1,16 @@ +import { Value } from '@sinclair/typebox/value' +import { Type } from '@sinclair/typebox' +import { Assert } from '../../assert/index' + +describe('value/default/Function', () => { + it('Should use default', () => { + const T = Type.Function([], Type.Number(), { default: 1 }) + const R = Value.Default(T, undefined) + Assert.IsEqual(R, 1) + }) + it('Should use value', () => { + const T = Type.Function([], Type.Number(), { default: 1 }) + const R = Value.Default(T, null) + Assert.IsEqual(R, null) + }) +}) diff --git a/test/runtime/value/default/index.ts b/test/runtime/value/default/index.ts new file mode 100644 index 000000000..7395f9fce --- /dev/null +++ b/test/runtime/value/default/index.ts @@ -0,0 +1,35 @@ +import './any' +import './array' +import './async-iterator' +import './bigint' +import './boolean' +import './composite' +import './constructor' +import './date' +import './enum' +import './function' +import './integer' +import './intersect' +import './iterator' +import './keyof' +import './kind' +import './literal' +import './never' +import './not' +import './null' +import './number' +import './object' +import './promise' +import './record' +import './recursive' +import './ref' +import './regexp' +import './string' +import './symbol' +import './template-literal' +import './tuple' +import './uint8array' +import './undefined' +import './union' +import './unknown' +import './void' diff --git a/test/runtime/value/default/integer.ts b/test/runtime/value/default/integer.ts new file mode 100644 index 000000000..c0ce64b4a --- /dev/null +++ b/test/runtime/value/default/integer.ts @@ -0,0 +1,16 @@ +import { Value } from '@sinclair/typebox/value' +import { Type } from '@sinclair/typebox' +import { Assert } from '../../assert/index' + +describe('value/default/Integer', () => { + it('Should use default', () => { + const T = Type.Integer({ default: 1 }) + const R = Value.Default(T, undefined) + Assert.IsEqual(R, 1) + }) + it('Should use value', () => { + const T = Type.Integer({ default: 1 }) + const R = Value.Default(T, null) + Assert.IsEqual(R, null) + }) +}) diff --git a/test/runtime/value/default/intersect.ts b/test/runtime/value/default/intersect.ts new file mode 100644 index 000000000..0e72b3e6e --- /dev/null +++ b/test/runtime/value/default/intersect.ts @@ -0,0 +1,106 @@ +import { Value } from '@sinclair/typebox/value' +import { Type } from '@sinclair/typebox' +import { Assert } from '../../assert/index' + +describe('value/default/Intersect', () => { + it('Should use default', () => { + const T = Type.Intersect([Type.Number(), Type.String()], { default: 1 }) + const R = Value.Default(T, undefined) + Assert.IsEqual(R, 1) + }) + it('Should use value', () => { + const T = Type.Intersect([Type.Number(), Type.String()], { default: 1 }) + const R = Value.Default(T, null) + Assert.IsEqual(R, null) + }) + // ---------------------------------------------------------------- + // Intersected + // ---------------------------------------------------------------- + it('Should use default intersected 1', () => { + const A = Type.Object({ + a: Type.Number({ default: 1 }), + }) + const B = Type.Object({ + b: Type.Number({ default: 2 }), + }) + const T = Type.Intersect([A, B]) + const R = Value.Default(T, {}) + Assert.IsEqual(R, { a: 1, b: 2 }) + }) + it('Should use default intersected 2', () => { + const A = Type.Object({ + a: Type.Number(), + }) + const B = Type.Object({ + b: Type.Number(), + }) + const T = Type.Intersect([A, B]) + const R = Value.Default(T, {}) + Assert.IsEqual(R, {}) + }) + it('Should use default intersected 3', () => { + const A = Type.Object({ + a: Type.Number({ default: 1 }), + }) + const B = Type.Object({ + b: Type.Number({ default: 2 }), + }) + const T = Type.Intersect([A, B]) + const R = Value.Default(T, { a: 3 }) + Assert.IsEqual(R, { a: 3, b: 2 }) + }) + it('Should use default intersected 4', () => { + const A = Type.Object({ + a: Type.Number({ default: 1 }), + }) + const B = Type.Object({ + b: Type.Number({ default: 2 }), + }) + const T = Type.Intersect([A, B]) + const R = Value.Default(T, { a: 4, b: 5 }) + Assert.IsEqual(R, { a: 4, b: 5 }) + }) + it('Should use default intersected 5', () => { + const A = Type.Object({ + a: Type.Number({ default: 1 }), + }) + const B = Type.Number({ default: 2 }) + const T = Type.Intersect([A, B]) + const R = Value.Default(T, {}) + Assert.IsEqual(R, { a: 1 }) + }) + it('Should use default intersected 6', () => { + const A = Type.Number({ default: 2 }) + const B = Type.Object({ + a: Type.Number({ default: 1 }), + }) + const T = Type.Intersect([A, B]) + const R = Value.Default(T, {}) + Assert.IsEqual(R, { a: 1 }) + }) + // ---------------------------------------------------------------- + // Intersected Deep + // ---------------------------------------------------------------- + it('Should use default intersected deep 1', () => { + const A = Type.Object({ a: Type.Number({ default: 1 }) }) + const B = Type.Object({ b: Type.Number({ default: 2 }) }) + const C = Type.Object({ c: Type.Number({ default: 3 }) }) + const D = Type.Object({ d: Type.Number({ default: 4 }) }) + const T1 = Type.Intersect([A, B]) + const T2 = Type.Intersect([C, D]) + const T = Type.Intersect([T1, T2]) + const R = Value.Default(T, {}) + Assert.IsEqual(R, { a: 1, b: 2, c: 3, d: 4 }) + }) + it('Should use default intersected deep 2', () => { + const A = Type.Object({ a: Type.Number({}) }) + const B = Type.Object({ b: Type.Number({}) }) + const C = Type.Object({ c: Type.Number({}) }) + const D = Type.Object({ d: Type.Number({}) }) + const T1 = Type.Intersect([A, B]) + const T2 = Type.Intersect([C, D]) + const T = Type.Intersect([T1, T2]) + const R = Value.Default(T, {}) + Assert.IsEqual(R, {}) + }) +}) diff --git a/test/runtime/value/default/iterator.ts b/test/runtime/value/default/iterator.ts new file mode 100644 index 000000000..d49b7f296 --- /dev/null +++ b/test/runtime/value/default/iterator.ts @@ -0,0 +1,16 @@ +import { Value } from '@sinclair/typebox/value' +import { Type } from '@sinclair/typebox' +import { Assert } from '../../assert/index' + +describe('value/default/Iterator', () => { + it('Should use default', () => { + const T = Type.Iterator(Type.Number(), { default: 1 }) + const R = Value.Default(T, undefined) + Assert.IsEqual(R, 1) + }) + it('Should use value', () => { + const T = Type.Iterator(Type.Number(), { default: 1 }) + const R = Value.Default(T, null) + Assert.IsEqual(R, null) + }) +}) diff --git a/test/runtime/value/default/keyof.ts b/test/runtime/value/default/keyof.ts new file mode 100644 index 000000000..2cb0bb26b --- /dev/null +++ b/test/runtime/value/default/keyof.ts @@ -0,0 +1,16 @@ +import { Value } from '@sinclair/typebox/value' +import { Type } from '@sinclair/typebox' +import { Assert } from '../../assert/index' + +describe('value/default/KeyOf', () => { + it('Should use default', () => { + const T = Type.KeyOf(Type.Object({ x: Type.Number() }), { default: 1 }) + const R = Value.Default(T, undefined) + Assert.IsEqual(R, 1) + }) + it('Should use value', () => { + const T = Type.KeyOf(Type.Object({ x: Type.Number() }), { default: 1 }) + const R = Value.Default(T, null) + Assert.IsEqual(R, null) + }) +}) diff --git a/test/runtime/value/default/kind.ts b/test/runtime/value/default/kind.ts new file mode 100644 index 000000000..1f8a96fcc --- /dev/null +++ b/test/runtime/value/default/kind.ts @@ -0,0 +1,16 @@ +import { Value } from '@sinclair/typebox/value' +import { Type, Kind } from '@sinclair/typebox' +import { Assert } from '../../assert/index' + +describe('value/default/Kind', () => { + it('Should use default', () => { + const T = Type.Unsafe({ [Kind]: 'Unknown', default: 1 }) + const R = Value.Default(T, undefined) + Assert.IsEqual(R, 1) + }) + it('Should use value', () => { + const T = Type.Unsafe({ [Kind]: 'Unknown', default: 1 }) + const R = Value.Default(T, null) + Assert.IsEqual(R, null) + }) +}) diff --git a/test/runtime/value/default/literal.ts b/test/runtime/value/default/literal.ts new file mode 100644 index 000000000..fa01d55b9 --- /dev/null +++ b/test/runtime/value/default/literal.ts @@ -0,0 +1,16 @@ +import { Value } from '@sinclair/typebox/value' +import { Type } from '@sinclair/typebox' +import { Assert } from '../../assert/index' + +describe('value/default/Literal', () => { + it('Should use default', () => { + const T = Type.Literal('foo', { default: 1 }) + const R = Value.Default(T, undefined) + Assert.IsEqual(R, 1) + }) + it('Should use value', () => { + const T = Type.Literal('foo', { default: 1 }) + const R = Value.Default(T, null) + Assert.IsEqual(R, null) + }) +}) diff --git a/test/runtime/value/default/never.ts b/test/runtime/value/default/never.ts new file mode 100644 index 000000000..a39542739 --- /dev/null +++ b/test/runtime/value/default/never.ts @@ -0,0 +1,16 @@ +import { Value } from '@sinclair/typebox/value' +import { Type } from '@sinclair/typebox' +import { Assert } from '../../assert/index' + +describe('value/default/Never', () => { + it('Should use default', () => { + const T = Type.Never({ default: 1 }) + const R = Value.Default(T, undefined) + Assert.IsEqual(R, 1) + }) + it('Should use value', () => { + const T = Type.Never({ default: 1 }) + const R = Value.Default(T, null) + Assert.IsEqual(R, null) + }) +}) diff --git a/test/runtime/value/default/not.ts b/test/runtime/value/default/not.ts new file mode 100644 index 000000000..148756d5b --- /dev/null +++ b/test/runtime/value/default/not.ts @@ -0,0 +1,16 @@ +import { Value } from '@sinclair/typebox/value' +import { Type } from '@sinclair/typebox' +import { Assert } from '../../assert/index' + +describe('value/default/Not', () => { + it('Should use default', () => { + const T = Type.Not(Type.String(), { default: 1 }) + const R = Value.Default(T, undefined) + Assert.IsEqual(R, 1) + }) + it('Should use value', () => { + const T = Type.Not(Type.String(), { default: 1 }) + const R = Value.Default(T, null) + Assert.IsEqual(R, null) + }) +}) diff --git a/test/runtime/value/default/null.ts b/test/runtime/value/default/null.ts new file mode 100644 index 000000000..9246fe893 --- /dev/null +++ b/test/runtime/value/default/null.ts @@ -0,0 +1,16 @@ +import { Value } from '@sinclair/typebox/value' +import { Type } from '@sinclair/typebox' +import { Assert } from '../../assert/index' + +describe('value/default/Null', () => { + it('Should use default', () => { + const T = Type.Null({ default: 1 }) + const R = Value.Default(T, undefined) + Assert.IsEqual(R, 1) + }) + it('Should use value', () => { + const T = Type.Null({ default: 1 }) + const R = Value.Default(T, null) + Assert.IsEqual(R, null) + }) +}) diff --git a/test/runtime/value/default/number.ts b/test/runtime/value/default/number.ts new file mode 100644 index 000000000..6dcaa7035 --- /dev/null +++ b/test/runtime/value/default/number.ts @@ -0,0 +1,16 @@ +import { Value } from '@sinclair/typebox/value' +import { Type } from '@sinclair/typebox' +import { Assert } from '../../assert/index' + +describe('value/default/Number', () => { + it('Should use default', () => { + const T = Type.Number({ default: 1 }) + const R = Value.Default(T, undefined) + Assert.IsEqual(R, 1) + }) + it('Should use value', () => { + const T = Type.Number({ default: 1 }) + const R = Value.Default(T, null) + Assert.IsEqual(R, null) + }) +}) diff --git a/test/runtime/value/default/object.ts b/test/runtime/value/default/object.ts new file mode 100644 index 000000000..ab4dda473 --- /dev/null +++ b/test/runtime/value/default/object.ts @@ -0,0 +1,180 @@ +import { Value } from '@sinclair/typebox/value' +import { Type } from '@sinclair/typebox' +import { Assert } from '../../assert/index' + +describe('value/default/Object', () => { + it('Should use default', () => { + const T = Type.Object( + { + x: Type.Number(), + y: Type.Number(), + }, + { default: 1 }, + ) + const R = Value.Default(T, undefined) + Assert.IsEqual(R, 1) + }) + it('Should use value', () => { + const T = Type.Object( + { + x: Type.Number(), + y: Type.Number(), + }, + { default: 1 }, + ) + const R = Value.Default(T, null) + Assert.IsEqual(R, null) + }) + // ---------------------------------------------------------------- + // Construction + // ---------------------------------------------------------------- + it('Should should fully construct object 1', () => { + const T = Type.Object( + { + x: Type.Object( + { + x: Type.Number({ default: 1 }), + y: Type.Number({ default: 2 }), + }, + { default: {} }, + ), + y: Type.Object( + { + x: Type.Number({ default: 3 }), + y: Type.Number({ default: 4 }), + }, + { default: {} }, + ), + }, + { default: {} }, + ) + const R = Value.Default(T, undefined) + Assert.IsEqual(R, { x: { x: 1, y: 2 }, y: { x: 3, y: 4 } }) + }) + it('Should should fully construct object 2', () => { + const T = Type.Object( + { + x: Type.Object( + { + x: Type.Number({ default: 1 }), + y: Type.Number({ default: 2 }), + }, + { default: {} }, + ), + y: Type.Object( + { + x: Type.Number({ default: 3 }), + y: Type.Number({ default: 4 }), + }, + { default: {} }, + ), + }, + { default: {} }, + ) + const R = Value.Default(T, { x: null }) + Assert.IsEqual(R, { x: null, y: { x: 3, y: 4 } }) + }) + it('Should should fully construct object 3', () => { + const T = Type.Object( + { + x: Type.Object( + { + x: Type.Number({ default: 1 }), + y: Type.Number({ default: 2 }), + }, + { default: {} }, + ), + y: Type.Object( + { + x: Type.Number({ default: 3 }), + y: Type.Number({ default: 4 }), + }, + { default: {} }, + ), + }, + { default: {} }, + ) + const R = Value.Default(T, { x: { x: null, y: null } }) + Assert.IsEqual(R, { x: { x: null, y: null }, y: { x: 3, y: 4 } }) + }) + // ---------------------------------------------------------------- + // Properties + // ---------------------------------------------------------------- + it('Should use property defaults 1', () => { + const T = Type.Object( + { + x: Type.Number({ default: 1 }), + y: Type.Number({ default: 2 }), + }, + { default: 1 }, + ) + const R = Value.Default(T, {}) + Assert.IsEqual(R, { x: 1, y: 2 }) + }) + it('Should use property defaults 2', () => { + const T = Type.Object({ + x: Type.Number(), + y: Type.Number(), + }) + const R = Value.Default(T, {}) + Assert.IsEqual(R, {}) + }) + it('Should use property defaults 3', () => { + const T = Type.Object({ + x: Type.Number({ default: 1 }), + y: Type.Number(), + }) + const R = Value.Default(T, {}) + Assert.IsEqual(R, { x: 1 }) + }) + it('Should use property defaults 4', () => { + const T = Type.Object({ + x: Type.Number({ default: 1 }), + y: Type.Number(), + }) + const R = Value.Default(T, { x: 3 }) + Assert.IsEqual(R, { x: 3 }) + }) + // ---------------------------------------------------------------- + // AdditionalProperties + // ---------------------------------------------------------------- + it('Should use additional property defaults 1', () => { + const T = Type.Object( + { + x: Type.Number({ default: 1 }), + y: Type.Number({ default: 2 }), + }, + { + additionalProperties: Type.Number({ default: 3 }), + }, + ) + const R = Value.Default(T, {}) + Assert.IsEqual(R, { x: 1, y: 2 }) + }) + it('Should use additional property defaults 2', () => { + const T = Type.Object( + { + x: Type.Number({ default: 1 }), + y: Type.Number({ default: 2 }), + }, + { + additionalProperties: Type.Number({ default: 3 }), + }, + ) + const R = Value.Default(T, { x: null, y: null, z: undefined }) + Assert.IsEqual(R, { x: null, y: null, z: 3 }) + }) + it('Should use additional property defaults 3', () => { + const T = Type.Object( + { + x: Type.Number({ default: 1 }), + y: Type.Number({ default: 2 }), + }, + { + additionalProperties: Type.Number(), + }, + ) + const R = Value.Default(T, { x: null, y: null, z: undefined }) + Assert.IsEqual(R, { x: null, y: null, z: undefined }) + }) +}) diff --git a/test/runtime/value/default/promise.ts b/test/runtime/value/default/promise.ts new file mode 100644 index 000000000..659e052cb --- /dev/null +++ b/test/runtime/value/default/promise.ts @@ -0,0 +1,16 @@ +import { Value } from '@sinclair/typebox/value' +import { Type } from '@sinclair/typebox' +import { Assert } from '../../assert/index' + +describe('value/default/Promise', () => { + it('Should use default', () => { + const T = Type.Any({ default: 1 }) + const R = Value.Default(T, undefined) + Assert.IsEqual(R, 1) + }) + it('Should use value', () => { + const T = Type.Any({ default: 1 }) + const R = Value.Default(T, null) + Assert.IsEqual(R, null) + }) +}) diff --git a/test/runtime/value/default/record.ts b/test/runtime/value/default/record.ts new file mode 100644 index 000000000..4b300ddc2 --- /dev/null +++ b/test/runtime/value/default/record.ts @@ -0,0 +1,66 @@ +import { Value } from '@sinclair/typebox/value' +import { Type } from '@sinclair/typebox' +import { Assert } from '../../assert/index' + +describe('value/default/Record', () => { + it('Should use default', () => { + const T = Type.Record(Type.String(), Type.Number(), { default: 1 }) + const R = Value.Default(T, undefined) + Assert.IsEqual(R, 1) + }) + it('Should use value', () => { + const T = Type.Record(Type.String(), Type.Number(), { default: 1 }) + const R = Value.Default(T, null) + Assert.IsEqual(R, null) + }) + // // ---------------------------------------------------------------- + // // Properties + // // ---------------------------------------------------------------- + it('Should use property defaults 1', () => { + const T = Type.Record(Type.Number(), Type.Number({ default: 1 })) + const R = Value.Default(T, { 0: undefined }) + Assert.IsEqual(R, { 0: 1 }) + }) + it('Should use property defaults 2', () => { + const T = Type.Record(Type.Number(), Type.Number({ default: 1 })) + const R = Value.Default(T, { 0: null }) + Assert.IsEqual(R, { 0: null }) + }) + it('Should use property defaults 3', () => { + const T = Type.Record(Type.Number(), Type.Number({ default: 1 })) + const R = Value.Default(T, { a: undefined }) + Assert.IsEqual(R, { a: undefined }) + }) + it('Should use property defaults 4', () => { + const T = Type.Record(Type.Number(), Type.Number({ default: 1 })) + const R = Value.Default(T, { 0: undefined }) + Assert.IsEqual(R, { 0: 1 }) + }) + it('Should use property defaults 5', () => { + const T = Type.Record(Type.Number(), Type.Number()) + const R = Value.Default(T, { 0: undefined }) + Assert.IsEqual(R, { 0: undefined }) + }) + it('Should use property defaults 6', () => { + const T = Type.Record(Type.Number(), Type.Number({ default: 1 })) + const R = Value.Default(T, {}) + Assert.IsEqual(R, {}) + }) + // ---------------------------------------------------------------- + // Additional Properties + // ---------------------------------------------------------------- + it('Should use additional property defaults 1', () => { + const T = Type.Record(Type.Number(), Type.Number({ default: 1 }), { + additionalProperties: Type.Number({ default: 3 }), + }) + const R = Value.Default(T, { 0: undefined, a: undefined }) + Assert.IsEqual(R, { 0: 1, a: 3 }) + }) + it('Should use additional property defaults 2', () => { + const T = Type.Record(Type.Number(), Type.Number({ default: 1 }), { + additionalProperties: Type.Number(), + }) + const R = Value.Default(T, { 0: undefined, a: undefined }) + Assert.IsEqual(R, { 0: 1, a: undefined }) + }) +}) diff --git a/test/runtime/value/default/recursive.ts b/test/runtime/value/default/recursive.ts new file mode 100644 index 000000000..417727bda --- /dev/null +++ b/test/runtime/value/default/recursive.ts @@ -0,0 +1,58 @@ +import { Value } from '@sinclair/typebox/value' +import { Type } from '@sinclair/typebox' +import { Assert } from '../../assert/index' + +// prettier-ignore +describe('value/default/Recursive', () => { + it('Should use default', () => { + const T = Type.Recursive((This) => Type.Object({ + nodes: Type.Array(This) + }, { default: 1 })) + const R = Value.Default(T, undefined) + Assert.IsEqual(R, 1) + }) + it('Should use value', () => { + const T = Type.Recursive((This) => Type.Object({ + nodes: Type.Array(This) + }, { default: 1 })) + const R = Value.Default(T, null) + Assert.IsEqual(R, null) + }) + // ---------------------------------------------------------------- + // Recursive + // ---------------------------------------------------------------- + it('Should use default recursive values', () => { + const T = Type.Recursive((This) => Type.Object({ + id: Type.String({ default: 1 }), + nodes: Type.Array(This, { default: [] }) // need this + })) + const R = Value.Default(T, { + nodes: [{ + nodes: [{ + nodes: [{ id: null }] + }, { + nodes: [{ id: null }] + }] + }] + }) + Assert.IsEqual(R, { + nodes: [{ + nodes: [{ + nodes: [{ + id: null, + nodes: [] + }], + id: 1 + }, { + nodes: [{ + id: null, + nodes: [] + }], + id: 1 + }], + id: 1 + }], + id: 1 + }) + }) +}) diff --git a/test/runtime/value/default/ref.ts b/test/runtime/value/default/ref.ts new file mode 100644 index 000000000..634059573 --- /dev/null +++ b/test/runtime/value/default/ref.ts @@ -0,0 +1,33 @@ +import { Value } from '@sinclair/typebox/value' +import { Type } from '@sinclair/typebox' +import { Assert } from '../../assert/index' + +describe('value/default/Ref', () => { + it('Should use default', () => { + const A = Type.String({ $id: 'A' }) + const T = Type.Ref('A', { default: 1 }) + const R = Value.Default(T, [A], undefined) + Assert.IsEqual(R, 1) + }) + it('Should use value', () => { + const A = Type.String({ $id: 'A' }) + const T = Type.Ref('A', { default: 1 }) + const R = Value.Default(T, [A], null) + Assert.IsEqual(R, null) + }) + // ---------------------------------------------------------------- + // Foreign + // ---------------------------------------------------------------- + it('Should use default on foreign value', () => { + const A = Type.String({ $id: 'A', default: 1 }) + const T = Type.Ref('A') + const R = Value.Default(T, [A], undefined) + Assert.IsEqual(R, 1) + }) + it('Should use value on foreign value', () => { + const A = Type.String({ $id: 'A', default: 1 }) + const T = Type.Ref('A') + const R = Value.Default(T, [A], null) + Assert.IsEqual(R, null) + }) +}) diff --git a/test/runtime/value/default/regexp.ts b/test/runtime/value/default/regexp.ts new file mode 100644 index 000000000..9c32c7ab2 --- /dev/null +++ b/test/runtime/value/default/regexp.ts @@ -0,0 +1,16 @@ +import { Value } from '@sinclair/typebox/value' +import { Type } from '@sinclair/typebox' +import { Assert } from '../../assert/index' + +describe('value/default/RegExp', () => { + it('Should use default', () => { + const T = Type.RegExp('', { default: 1 }) + const R = Value.Default(T, undefined) + Assert.IsEqual(R, 1) + }) + it('Should use value', () => { + const T = Type.RegExp('', { default: 1 }) + const R = Value.Default(T, null) + Assert.IsEqual(R, null) + }) +}) diff --git a/test/runtime/value/default/string.ts b/test/runtime/value/default/string.ts new file mode 100644 index 000000000..af833332e --- /dev/null +++ b/test/runtime/value/default/string.ts @@ -0,0 +1,16 @@ +import { Value } from '@sinclair/typebox/value' +import { Type } from '@sinclair/typebox' +import { Assert } from '../../assert/index' + +describe('value/default/String', () => { + it('Should use default', () => { + const T = Type.String({ default: 1 }) + const R = Value.Default(T, undefined) + Assert.IsEqual(R, 1) + }) + it('Should use value', () => { + const T = Type.String({ default: 1 }) + const R = Value.Default(T, null) + Assert.IsEqual(R, null) + }) +}) diff --git a/test/runtime/value/default/symbol.ts b/test/runtime/value/default/symbol.ts new file mode 100644 index 000000000..d593fb7b8 --- /dev/null +++ b/test/runtime/value/default/symbol.ts @@ -0,0 +1,16 @@ +import { Value } from '@sinclair/typebox/value' +import { Type } from '@sinclair/typebox' +import { Assert } from '../../assert/index' + +describe('value/default/Symbol', () => { + it('Should use default', () => { + const T = Type.Symbol({ default: 1 }) + const R = Value.Default(T, undefined) + Assert.IsEqual(R, 1) + }) + it('Should use value', () => { + const T = Type.Symbol({ default: 1 }) + const R = Value.Default(T, null) + Assert.IsEqual(R, null) + }) +}) diff --git a/test/runtime/value/default/template-literal.ts b/test/runtime/value/default/template-literal.ts new file mode 100644 index 000000000..2dd6b7e83 --- /dev/null +++ b/test/runtime/value/default/template-literal.ts @@ -0,0 +1,16 @@ +import { Value } from '@sinclair/typebox/value' +import { Type } from '@sinclair/typebox' +import { Assert } from '../../assert/index' + +describe('value/default/TemplateLiteral', () => { + it('Should use default', () => { + const T = Type.TemplateLiteral('hello', { default: 1 }) + const R = Value.Default(T, undefined) + Assert.IsEqual(R, 1) + }) + it('Should use value', () => { + const T = Type.TemplateLiteral('hello', { default: 1 }) + const R = Value.Default(T, null) + Assert.IsEqual(R, null) + }) +}) diff --git a/test/runtime/value/default/tuple.ts b/test/runtime/value/default/tuple.ts new file mode 100644 index 000000000..c7acdc99b --- /dev/null +++ b/test/runtime/value/default/tuple.ts @@ -0,0 +1,44 @@ +import { Value } from '@sinclair/typebox/value' +import { Type } from '@sinclair/typebox' +import { Assert } from '../../assert/index' + +describe('value/default/Tuple', () => { + it('Should use default', () => { + const T = Type.Tuple([Type.Number(), Type.Number()], { default: 1 }) + const R = Value.Default(T, undefined) + Assert.IsEqual(R, 1) + }) + it('Should use value', () => { + const T = Type.Tuple([Type.Number(), Type.Number()], { default: 1 }) + const R = Value.Default(T, null) + Assert.IsEqual(R, null) + }) + // ---------------------------------------------------------------- + // Elements + // ---------------------------------------------------------------- + it('Should use default elements 1', () => { + const T = Type.Tuple([Type.Number({ default: 1 }), Type.Number({ default: 2 })], { default: [] }) + const R = Value.Default(T, null) + Assert.IsEqual(R, null) + }) + it('Should use default elements 2', () => { + const T = Type.Tuple([Type.Number({ default: 1 }), Type.Number({ default: 2 })], { default: [] }) + const R = Value.Default(T, undefined) + Assert.IsEqual(R, [1, 2]) + }) + it('Should use default elements 3', () => { + const T = Type.Tuple([Type.Number({ default: 1 }), Type.Number({ default: 2 })], { default: [] }) + const R = Value.Default(T, [4, 5, 6]) + Assert.IsEqual(R, [4, 5, 6]) + }) + it('Should use default elements 4', () => { + const T = Type.Tuple([Type.Number({ default: 1 }), Type.Number({ default: 2 })]) + const R = Value.Default(T, [4, 5, 6]) + Assert.IsEqual(R, [4, 5, 6]) + }) + it('Should use default elements 5', () => { + const T = Type.Tuple([Type.Number({ default: 1 }), Type.Number({ default: 2 })]) + const R = Value.Default(T, [4]) + Assert.IsEqual(R, [4, 2]) + }) +}) diff --git a/test/runtime/value/default/uint8array.ts b/test/runtime/value/default/uint8array.ts new file mode 100644 index 000000000..5c3cfa77d --- /dev/null +++ b/test/runtime/value/default/uint8array.ts @@ -0,0 +1,16 @@ +import { Value } from '@sinclair/typebox/value' +import { Type } from '@sinclair/typebox' +import { Assert } from '../../assert/index' + +describe('value/default/Uint8Array', () => { + it('Should use default', () => { + const T = Type.Uint8Array({ default: 1 }) + const R = Value.Default(T, undefined) + Assert.IsEqual(R, 1) + }) + it('Should use value', () => { + const T = Type.Uint8Array({ default: 1 }) + const R = Value.Default(T, null) + Assert.IsEqual(R, null) + }) +}) diff --git a/test/runtime/value/default/undefined.ts b/test/runtime/value/default/undefined.ts new file mode 100644 index 000000000..d622cd1c6 --- /dev/null +++ b/test/runtime/value/default/undefined.ts @@ -0,0 +1,16 @@ +import { Value } from '@sinclair/typebox/value' +import { Type } from '@sinclair/typebox' +import { Assert } from '../../assert/index' + +describe('value/default/Undefined', () => { + it('Should use default', () => { + const T = Type.Undefined({ default: 1 }) + const R = Value.Default(T, undefined) + Assert.IsEqual(R, 1) + }) + it('Should use value', () => { + const T = Type.Undefined({ default: 1 }) + const R = Value.Default(T, null) + Assert.IsEqual(R, null) + }) +}) diff --git a/test/runtime/value/default/union.ts b/test/runtime/value/default/union.ts new file mode 100644 index 000000000..56b572caf --- /dev/null +++ b/test/runtime/value/default/union.ts @@ -0,0 +1,110 @@ +import { Value } from '@sinclair/typebox/value' +import { Type } from '@sinclair/typebox' +import { Assert } from '../../assert/index' + +describe('value/default/Union', () => { + it('Should use default', () => { + const T = Type.Union([Type.Number(), Type.String()], { default: 1 }) + const R = Value.Default(T, undefined) + Assert.IsEqual(R, 1) + }) + it('Should use value', () => { + const T = Type.Union([Type.Number(), Type.String()], { default: 1 }) + const R = Value.Default(T, null) + Assert.IsEqual(R, null) + }) + // ---------------------------------------------------------------- + // Interior + // ---------------------------------------------------------------- + it('Should default interior 1', () => { + const T = Type.Union([ + Type.Object({ + x: Type.Number({ default: 1 }), + y: Type.Number({ default: 2 }), + }), + Type.String({ default: 'hello' }), + ]) + const R = Value.Default(T, null) + Assert.IsEqual(R, null) + }) + it('Should default interior 2', () => { + const T = Type.Union([ + Type.Object({ + x: Type.Number({ default: 1 }), + y: Type.Number({ default: 2 }), + }), + Type.String({ default: 'hello' }), + ]) + const R = Value.Default(T, undefined) + Assert.IsEqual(R, 'hello') + }) + it('Should default interior 3', () => { + const T = Type.Union([ + Type.Object({ + x: Type.Number({ default: 1 }), + y: Type.Number({ default: 2 }), + }), + Type.String({ default: 'hello' }), + ]) + const R = Value.Default(T, 'world') + Assert.IsEqual(R, 'world') + }) + it('Should default interior 4', () => { + const T = Type.Union([ + Type.Object({ + x: Type.Number({ default: 1 }), + y: Type.Number({ default: 2 }), + }), + Type.String({ default: 'hello' }), + ]) + const R = Value.Default(T, {}) + Assert.IsEqual(R, { x: 1, y: 2 }) + }) + it('Should default interior 5', () => { + const T = Type.Union([ + Type.Object({ + x: Type.Number({ default: 1 }), + y: Type.Number({ default: 2 }), + }), + Type.String({ default: 'hello' }), + ]) + const R = Value.Default(T, { x: 3 }) + Assert.IsEqual(R, { x: 3, y: 2 }) + }) + it('Should default interior 6', () => { + const T = Type.Union([ + Type.Object({ + x: Type.Number({ default: 1 }), + y: Type.Number({ default: 2 }), + }), + Type.String({ default: 'hello' }), + ]) + const R = Value.Default(T, { x: 3, y: 4 }) + Assert.IsEqual(R, { x: 3, y: 4 }) + }) + // ---------------------------------------------------------------- + // Interior Unsafe + // ---------------------------------------------------------------- + it('Should default interior unsafe 1', () => { + const T = Type.Union([ + Type.Object({ + x: Type.Number({ default: 1 }), + y: Type.Number({ default: 2 }), + }), + Type.Unsafe({ default: 'hello' }), + ]) + const R = Value.Default(T, undefined) + Assert.IsEqual(R, undefined) + }) + it('Should default interior unsafe 2', () => { + const T = Type.Union([ + Type.Object({ + x: Type.Number({ default: 1 }), + y: Type.Number({ default: 2 }), + }), + Type.Unsafe({ default: 'hello' }), + ]) + const R = Value.Default(T, 'world') + Assert.IsEqual(R, 'world') + }) +}) diff --git a/test/runtime/value/default/unknown.ts b/test/runtime/value/default/unknown.ts new file mode 100644 index 000000000..97236570d --- /dev/null +++ b/test/runtime/value/default/unknown.ts @@ -0,0 +1,16 @@ +import { Value } from '@sinclair/typebox/value' +import { Type } from '@sinclair/typebox' +import { Assert } from '../../assert/index' + +describe('value/default/Unknown', () => { + it('Should use default', () => { + const T = Type.Unknown({ default: 1 }) + const R = Value.Default(T, undefined) + Assert.IsEqual(R, 1) + }) + it('Should use value', () => { + const T = Type.Unknown({ default: 1 }) + const R = Value.Default(T, null) + Assert.IsEqual(R, null) + }) +}) diff --git a/test/runtime/value/default/void.ts b/test/runtime/value/default/void.ts new file mode 100644 index 000000000..03d03c1e3 --- /dev/null +++ b/test/runtime/value/default/void.ts @@ -0,0 +1,16 @@ +import { Value } from '@sinclair/typebox/value' +import { Type } from '@sinclair/typebox' +import { Assert } from '../../assert/index' + +describe('value/default/Void', () => { + it('Should use default', () => { + const T = Type.Void({ default: 1 }) + const R = Value.Default(T, undefined) + Assert.IsEqual(R, 1) + }) + it('Should use value', () => { + const T = Type.Void({ default: 1 }) + const R = Value.Default(T, null) + Assert.IsEqual(R, null) + }) +}) diff --git a/test/runtime/value/index.ts b/test/runtime/value/index.ts index de126d75d..b795b7a55 100644 --- a/test/runtime/value/index.ts +++ b/test/runtime/value/index.ts @@ -3,6 +3,7 @@ import './check' import './clone' import './convert' import './create' +import './default' import './delta' import './equal' import './guard'