From 1d5cd5b17bef13c6826762f8b1ce4f1d357611c8 Mon Sep 17 00:00:00 2001 From: sinclair Date: Sat, 18 Nov 2023 17:49:25 +0900 Subject: [PATCH] Tuple Transform Check | Check Both Value and Compiler Encoding in Tests --- src/compiler/compiler.ts | 4 +- src/value/transform.ts | 19 +++--- src/value/value.ts | 12 ++-- test/runtime/value/transform/_encoder.ts | 33 +++++++++++ test/runtime/value/transform/any.ts | 9 +-- test/runtime/value/transform/array.ts | 31 +++++----- .../runtime/value/transform/async-iterator.ts | 13 ++-- test/runtime/value/transform/bigint.ts | 13 ++-- test/runtime/value/transform/boolean.ts | 13 ++-- test/runtime/value/transform/constructor.ts | 13 ++-- test/runtime/value/transform/date.ts | 13 ++-- test/runtime/value/transform/enum.ts | 13 ++-- test/runtime/value/transform/function.ts | 13 ++-- test/runtime/value/transform/integer.ts | 13 ++-- test/runtime/value/transform/intersect.ts | 31 +++++----- test/runtime/value/transform/iterator.ts | 13 ++-- test/runtime/value/transform/literal.ts | 23 ++++---- test/runtime/value/transform/never.ts | 9 +-- test/runtime/value/transform/not.ts | 13 ++-- test/runtime/value/transform/null.ts | 13 ++-- test/runtime/value/transform/number.ts | 13 ++-- test/runtime/value/transform/object.ts | 39 ++++++------ test/runtime/value/transform/promise.ts | 13 ++-- test/runtime/value/transform/record.ts | 25 ++++---- test/runtime/value/transform/recursive.ts | 19 +++--- test/runtime/value/transform/ref.ts | 19 +++--- test/runtime/value/transform/string.ts | 35 +++++------ test/runtime/value/transform/symbol.ts | 13 ++-- .../value/transform/template-literal.ts | 13 ++-- test/runtime/value/transform/tuple.ts | 31 +++++----- test/runtime/value/transform/undefined.ts | 13 ++-- test/runtime/value/transform/union.ts | 59 ++++++++++--------- test/runtime/value/transform/unknown.ts | 9 +-- test/runtime/value/transform/unsafe.ts | 13 ++-- 34 files changed, 344 insertions(+), 284 deletions(-) create mode 100644 test/runtime/value/transform/_encoder.ts diff --git a/src/compiler/compiler.ts b/src/compiler/compiler.ts index 9e840ca92..fe03f6d2e 100644 --- a/src/compiler/compiler.ts +++ b/src/compiler/compiler.ts @@ -61,11 +61,11 @@ export class TypeCheck { /** Decodes a value or throws if error */ public Decode(value: unknown): Types.StaticDecode { if (!this.checkFunc(value)) throw new TransformDecodeCheckError(this.schema, value, this.Errors(value).First()!) - return this.hasTransform ? DecodeTransform.Decode(this.schema, this.references, value, (_, __, value) => this.Check(value)) : value + return this.hasTransform ? DecodeTransform.Decode(this.schema, this.references, value) : value } /** Encodes a value or throws if error */ public Encode(value: unknown): Types.StaticEncode { - const encoded = this.hasTransform ? EncodeTransform.Encode(this.schema, this.references, value, (_, __, value) => this.Check(value)) : value + const encoded = this.hasTransform ? EncodeTransform.Encode(this.schema, this.references, value) : value if (!this.checkFunc(encoded)) throw new TransformEncodeCheckError(this.schema, value, this.Errors(value).First()!) return encoded } diff --git a/src/value/transform.ts b/src/value/transform.ts index 281917f18..d0c7daba5 100644 --- a/src/value/transform.ts +++ b/src/value/transform.ts @@ -26,9 +26,10 @@ THE SOFTWARE. ---------------------------------------------------------------------------*/ -import { IsString, IsPlainObject, IsArray, IsValueType } from './guard' +import { IsString, IsPlainObject, IsArray, IsValueType, IsUndefined } from './guard' import { ValueError } from '../errors/errors' import { Deref } from './deref' +import { Check } from './check' import * as Types from '../typebox' // ------------------------------------------------------------------------- @@ -112,7 +113,7 @@ export namespace HasTransform { return Visit(Deref(schema, references), references) } function TTuple(schema: Types.TTuple, references: Types.TSchema[]) { - return Types.TypeGuard.TTransform(schema) || (Types.TypeGuard.TSchema(schema.items) && schema.items.some((schema) => Visit(schema, references))) + return Types.TypeGuard.TTransform(schema) || (!IsUndefined(schema.items) && schema.items.some((schema) => Visit(schema, references))) } function TUnion(schema: Types.TUnion, references: Types.TSchema[]) { return Types.TypeGuard.TTransform(schema) || schema.anyOf.some((schema) => Visit(schema, references)) @@ -262,7 +263,7 @@ export namespace DecodeTransform { function TUnion(schema: Types.TUnion, references: Types.TSchema[], value: any) { const value1 = Default(schema, value) for (const subschema of schema.anyOf) { - if (!checkFunction(subschema, references, value1)) continue + if (!Check(subschema, references, value1)) continue return Visit(subschema, references, value1) } return value1 @@ -323,9 +324,7 @@ export namespace DecodeTransform { return Default(schema_, value) } } - let checkFunction: CheckFunction = () => false - export function Decode(schema: Types.TSchema, references: Types.TSchema[], value: unknown, check: CheckFunction): unknown { - checkFunction = check + export function Decode(schema: Types.TSchema, references: Types.TSchema[], value: unknown): unknown { return Visit(schema, references, value) } } @@ -405,14 +404,14 @@ export namespace EncodeTransform { function TUnion(schema: Types.TUnion, references: Types.TSchema[], value: any) { // test value against union variants for (const subschema of schema.anyOf) { - if (!checkFunction(subschema, references, value)) continue + if (!Check(subschema, references, value)) continue const value1 = Visit(subschema, references, value) return Default(schema, value1) } // test transformed value against union variants for (const subschema of schema.anyOf) { const value1 = Visit(subschema, references, value) - if (!checkFunction(schema, references, value1)) continue + if (!Check(schema, references, value1)) continue return Default(schema, value1) } return Default(schema, value) @@ -472,9 +471,7 @@ export namespace EncodeTransform { return Default(schema_, value) } } - let checkFunction: CheckFunction = () => false - export function Encode(schema: Types.TSchema, references: Types.TSchema[], value: unknown, check: CheckFunction): unknown { - checkFunction = check + export function Encode(schema: Types.TSchema, references: Types.TSchema[], value: unknown): unknown { return Visit(schema, references, value) } } diff --git a/src/value/value.ts b/src/value/value.ts index edeec7dae..9cf27168b 100644 --- a/src/value/value.ts +++ b/src/value/value.ts @@ -78,23 +78,23 @@ export namespace Value { return ValueClone.Clone(value) } /** Decodes a value or throws if error */ - export function Decode>(schema: T, references: Types.TSchema[], value: unknown): D + export function Decode>(schema: T, references: Types.TSchema[], value: unknown): R /** Decodes a value or throws if error */ - export function Decode>(schema: T, value: unknown): D + export function Decode>(schema: T, value: unknown): R /** Decodes a value or throws if error */ export function Decode(...args: any[]) { const [schema, references, value] = args.length === 3 ? [args[0], args[1], args[2]] : [args[0], [], args[1]] if (!Check(schema, references, value)) throw new ValueTransform.TransformDecodeCheckError(schema, value, Errors(schema, references, value).First()!) - return ValueTransform.DecodeTransform.Decode(schema, references, value, ValueCheck.Check) + return ValueTransform.DecodeTransform.Decode(schema, references, value) } /** Encodes a value or throws if error */ - export function Encode>(schema: T, references: Types.TSchema[], value: unknown): E + export function Encode>(schema: T, references: Types.TSchema[], value: unknown): R /** Encodes a value or throws if error */ - export function Encode>(schema: T, value: unknown): E + export function Encode>(schema: T, value: unknown): R /** Encodes a value or throws if error */ export function Encode(...args: any[]) { const [schema, references, value] = args.length === 3 ? [args[0], args[1], args[2]] : [args[0], [], args[1]] - const encoded = ValueTransform.EncodeTransform.Encode(schema, references, value, ValueCheck.Check) + const encoded = ValueTransform.EncodeTransform.Encode(schema, references, value) if (!Check(schema, references, encoded)) throw new ValueTransform.TransformEncodeCheckError(schema, value, Errors(schema, references, value).First()!) return encoded } diff --git a/test/runtime/value/transform/_encoder.ts b/test/runtime/value/transform/_encoder.ts new file mode 100644 index 000000000..8449787e6 --- /dev/null +++ b/test/runtime/value/transform/_encoder.ts @@ -0,0 +1,33 @@ +import { IsAsyncIterator, IsIterator, IsFunction, IsSymbol } from '@sinclair/typebox/value/guard' +import { TSchema, StaticDecode, StaticEncode } from '@sinclair/typebox' +import { TypeCompiler } from '@sinclair/typebox/compiler' +import { Value } from '@sinclair/typebox/value' +import { Assert } from '../../assert/index' + +function AssertSame(actual: unknown, expect: unknown) { + if (IsAsyncIterator(actual) && IsAsyncIterator(expect)) return + if (IsIterator(actual) && IsIterator(expect)) return + if (IsSymbol(actual) && IsSymbol(expect)) return + if (IsFunction(actual) && IsFunction(expect)) return + Assert.IsEqual(actual, expect) +} + +export function Decode>(schema: T, references: TSchema[], value: unknown): R +export function Decode>(schema: T, value: unknown): R +export function Decode(...args: any[]) { + const [schema, references, value] = args.length === 2 ? [args[0], [], args[1]] : [args[0], args[1], args[2]] + const value1 = TypeCompiler.Compile(schema as TSchema, references).Decode(value) + const value2 = Value.Decode(schema as TSchema, references, value) + AssertSame(value1, value2) + return value2 +} + +export function Encode>(schema: T, references: TSchema[], value: unknown): R +export function Encode>(schema: T, value: unknown): R +export function Encode(...args: any[]) { + const [schema, references, value] = args.length === 2 ? [args[0], [], args[1]] : [args[0], args[1], args[2]] + const value1 = TypeCompiler.Compile(schema as TSchema, references).Encode(value) + const value2 = Value.Encode(schema as TSchema, references, value) + AssertSame(value1, value2) + return value2 +} diff --git a/test/runtime/value/transform/any.ts b/test/runtime/value/transform/any.ts index 91281c315..a1b27e28c 100644 --- a/test/runtime/value/transform/any.ts +++ b/test/runtime/value/transform/any.ts @@ -1,3 +1,4 @@ +import * as Encoder from './_encoder' import { Assert } from '../../assert' import { Value } from '@sinclair/typebox/value' import { Type } from '@sinclair/typebox' @@ -10,11 +11,11 @@ describe('value/transform/Any', () => { .Decode((value) => value) .Encode((value) => value) it('Should decode mapped', () => { - const R = Value.Decode(T0, 123) + const R = Encoder.Decode(T0, 123) Assert.IsEqual(R, 123) }) it('Should encode mapped', () => { - const R = Value.Encode(T0, 123) + const R = Encoder.Encode(T0, 123) Assert.IsEqual(R, 123) }) // -------------------------------------------------------- @@ -24,11 +25,11 @@ describe('value/transform/Any', () => { .Decode((value) => 1) .Encode((value) => 2) it('Should decode mapped', () => { - const R = Value.Decode(T1, null) + const R = Encoder.Decode(T1, null) Assert.IsEqual(R, 1) }) it('Should encode mapped', () => { - const R = Value.Encode(T1, null) + const R = Encoder.Encode(T1, null) Assert.IsEqual(R, 2) }) }) diff --git a/test/runtime/value/transform/array.ts b/test/runtime/value/transform/array.ts index 96afb073e..8f688b2e8 100644 --- a/test/runtime/value/transform/array.ts +++ b/test/runtime/value/transform/array.ts @@ -1,3 +1,4 @@ +import * as Encoder from './_encoder' import { Assert } from '../../assert' import { Value } from '@sinclair/typebox/value' import { Type } from '@sinclair/typebox' @@ -10,15 +11,15 @@ describe('value/transform/Array', () => { .Decode((value) => value) .Encode((value) => value) it('Should decode mapped', () => { - const R = Value.Decode(T0, [0, 1, 2]) + const R = Encoder.Decode(T0, [0, 1, 2]) Assert.IsEqual(R, [0, 1, 2]) }) it('Should encode mapped', () => { - const R = Value.Encode(T0, [0, 1, 2]) + const R = Encoder.Encode(T0, [0, 1, 2]) Assert.IsEqual(R, [0, 1, 2]) }) it('Should throw on mapped decode', () => { - Assert.Throws(() => Value.Decode(T0, null)) + Assert.Throws(() => Encoder.Decode(T0, null)) }) // -------------------------------------------------------- // Mapped @@ -27,15 +28,15 @@ describe('value/transform/Array', () => { .Decode((value) => 1) .Encode((value) => [0, 1, 2]) it('Should decode mapped', () => { - const R = Value.Decode(T1, []) + const R = Encoder.Decode(T1, []) Assert.IsEqual(R, 1) }) it('Should encode mapped', () => { - const R = Value.Encode(T1, null) + const R = Encoder.Encode(T1, null) Assert.IsEqual(R, [0, 1, 2]) }) it('Should throw on mapped decode', () => { - Assert.Throws(() => Value.Decode(T1, null)) + Assert.Throws(() => Encoder.Decode(T1, null)) }) // -------------------------------------------------------- // Elements @@ -45,15 +46,15 @@ describe('value/transform/Array', () => { .Encode((value) => (value === 'TRUE' ? true : false)) const T2 = Type.Array(B2) it('Should decode elements', () => { - const R = Value.Decode(T2, [true, false]) + const R = Encoder.Decode(T2, [true, false]) Assert.IsEqual(R, ['TRUE', 'FALSE']) }) it('Should encode elements', () => { - const R = Value.Encode(T2, ['TRUE', 'FALSE']) + const R = Encoder.Encode(T2, ['TRUE', 'FALSE']) Assert.IsEqual(R, [true, false]) }) it('Should throw on elements decode', () => { - Assert.Throws(() => Value.Decode(T2, null)) + Assert.Throws(() => Encoder.Decode(T2, null)) }) // -------------------------------------------------------- // Elements Contains (Not Supported) @@ -65,14 +66,14 @@ describe('value/transform/Array', () => { contains: N3, }) it('Should decode contains', () => { - const R = Value.Decode(T3, [1, 2, 3]) + const R = Encoder.Decode(T3, [1, 2, 3]) Assert.IsEqual(R, [1, 2, 3]) }) it('Should throw on contains encode', () => { - Assert.Throws(() => Value.Encode(T3, ['hello', 2, 3])) + Assert.Throws(() => Encoder.Encode(T3, ['hello', 2, 3])) }) it('Should throw on contains decode', () => { - Assert.Throws(() => Value.Decode(T3, null)) + Assert.Throws(() => Encoder.Decode(T3, null)) }) // ------------------------------------------------------------ // Set @@ -81,17 +82,17 @@ describe('value/transform/Array', () => { .Decode((value) => new Set(value)) .Encode((value) => [...value]) it('should decode set', () => { - const R = Value.Decode(T4, [1, 1, 2, 3]) + const R = Encoder.Decode(T4, [1, 1, 2, 3]) Assert.IsInstanceOf(R, Set) Assert.IsTrue(R.has(1)) Assert.IsTrue(R.has(2)) Assert.IsTrue(R.has(3)) }) it('should encode set', () => { - const R = Value.Encode(T4, new Set([1, 2, 3])) + const R = Encoder.Encode(T4, new Set([1, 2, 3])) Assert.IsEqual(R, [1, 2, 3]) }) it('Should throw on set decode', () => { - Assert.Throws(() => Value.Decode(T4, {})) + Assert.Throws(() => Encoder.Decode(T4, {})) }) }) diff --git a/test/runtime/value/transform/async-iterator.ts b/test/runtime/value/transform/async-iterator.ts index c418d723f..aed1f3eb3 100644 --- a/test/runtime/value/transform/async-iterator.ts +++ b/test/runtime/value/transform/async-iterator.ts @@ -1,3 +1,4 @@ +import * as Encoder from './_encoder' import { Assert } from '../../assert' import { Value } from '@sinclair/typebox/value' import { Type } from '@sinclair/typebox' @@ -10,15 +11,15 @@ describe('value/transform/AsyncIterator', () => { .Decode((value) => value) .Encode((value) => value) it('Should decode identity', () => { - const R = Value.Decode(T0, (async function* (): any {})()) + const R = Encoder.Decode(T0, (async function* (): any {})()) Assert.IsTrue(Symbol.asyncIterator in R) }) it('Should encode identity', () => { - const R = Value.Encode(T0, (async function* (): any {})()) + const R = Encoder.Encode(T0, (async function* (): any {})()) Assert.IsTrue(Symbol.asyncIterator in R) }) it('Should throw on identity decode', () => { - Assert.Throws(() => Value.Decode(T0, null)) + Assert.Throws(() => Encoder.Decode(T0, null)) }) // -------------------------------------------------------- // Mapped @@ -27,14 +28,14 @@ describe('value/transform/AsyncIterator', () => { .Decode((value) => 1) .Encode((value) => (async function* (): any {})()) it('Should decode', () => { - const R = Value.Decode(T1, (async function* (): any {})()) + const R = Encoder.Decode(T1, (async function* (): any {})()) Assert.IsEqual(R, 1) }) it('Should encode', () => { - const R = Value.Encode(T1, null) + const R = Encoder.Encode(T1, null) Assert.IsTrue(Symbol.asyncIterator in R) }) it('Should throw on decode', () => { - Assert.Throws(() => Value.Decode(T1, null)) + Assert.Throws(() => Encoder.Decode(T1, null)) }) }) diff --git a/test/runtime/value/transform/bigint.ts b/test/runtime/value/transform/bigint.ts index ffc66b058..280af3114 100644 --- a/test/runtime/value/transform/bigint.ts +++ b/test/runtime/value/transform/bigint.ts @@ -1,3 +1,4 @@ +import * as Encoder from './_encoder' import { Assert } from '../../assert' import { Value } from '@sinclair/typebox/value' import { Type } from '@sinclair/typebox' @@ -10,15 +11,15 @@ describe('value/transform/BigInt', () => { .Decode((value) => value) .Encode((value) => value) it('Should decode identity', () => { - const R = Value.Decode(T0, 5n) + const R = Encoder.Decode(T0, 5n) Assert.IsEqual(R, 5n) }) it('Should encode identity', () => { - const R = Value.Encode(T0, 5n) + const R = Encoder.Encode(T0, 5n) Assert.IsEqual(R, 5n) }) it('Should throw on identity decode', () => { - Assert.Throws(() => Value.Decode(T0, null)) + Assert.Throws(() => Encoder.Decode(T0, null)) }) // -------------------------------------------------------- // Mapped @@ -27,14 +28,14 @@ describe('value/transform/BigInt', () => { .Decode((value) => 1) .Encode((value) => 2n) it('Should decode mapped', () => { - const R = Value.Decode(T1, 1n) + const R = Encoder.Decode(T1, 1n) Assert.IsEqual(R, 1) }) it('Should encode mapped', () => { - const R = Value.Encode(T1, null) + const R = Encoder.Encode(T1, null) Assert.IsEqual(R, 2n) }) it('Should throw on mapped decode', () => { - Assert.Throws(() => Value.Decode(T1, null)) + Assert.Throws(() => Encoder.Decode(T1, null)) }) }) diff --git a/test/runtime/value/transform/boolean.ts b/test/runtime/value/transform/boolean.ts index 1421a1e8a..ed9c47cee 100644 --- a/test/runtime/value/transform/boolean.ts +++ b/test/runtime/value/transform/boolean.ts @@ -1,3 +1,4 @@ +import * as Encoder from './_encoder' import { Assert } from '../../assert' import { Value } from '@sinclair/typebox/value' import { Type } from '@sinclair/typebox' @@ -10,15 +11,15 @@ describe('value/transform/Boolean', () => { .Decode((value) => value) .Encode((value) => value) it('Should decode identity', () => { - const R = Value.Decode(T0, true) + const R = Encoder.Decode(T0, true) Assert.IsEqual(R, true) }) it('Should encode identity', () => { - const R = Value.Encode(T0, false) + const R = Encoder.Encode(T0, false) Assert.IsEqual(R, false) }) it('Should throw on identity decode', () => { - Assert.Throws(() => Value.Decode(T0, null)) + Assert.Throws(() => Encoder.Decode(T0, null)) }) // -------------------------------------------------------- // Mapped @@ -27,14 +28,14 @@ describe('value/transform/Boolean', () => { .Decode((value) => 1) .Encode((value) => true) it('Should decode mapped', () => { - const R = Value.Decode(T1, true) + const R = Encoder.Decode(T1, true) Assert.IsEqual(R, 1) }) it('Should encode mapped', () => { - const R = Value.Encode(T1, null) + const R = Encoder.Encode(T1, null) Assert.IsEqual(R, true) }) it('Should throw on mapped decode', () => { - Assert.Throws(() => Value.Decode(T1, null)) + Assert.Throws(() => Encoder.Decode(T1, null)) }) }) diff --git a/test/runtime/value/transform/constructor.ts b/test/runtime/value/transform/constructor.ts index 0cfe28d43..67ae9d19a 100644 --- a/test/runtime/value/transform/constructor.ts +++ b/test/runtime/value/transform/constructor.ts @@ -1,3 +1,4 @@ +import * as Encoder from './_encoder' import { Assert } from '../../assert' import { Value } from '@sinclair/typebox/value' import { Type } from '@sinclair/typebox' @@ -10,15 +11,15 @@ describe('value/transform/Constructor', () => { .Decode((value) => value) .Encode((value) => value) it('Should decode identity', () => { - const R = Value.Decode(T0, class {}) + const R = Encoder.Decode(T0, class {}) Assert.IsTypeOf(R, 'function') }) it('Should encode identity', () => { - const R = Value.Encode(T0, class {}) + const R = Encoder.Encode(T0, class {}) Assert.IsTypeOf(R, 'function') }) it('Should throw on identity decode', () => { - Assert.Throws(() => Value.Decode(T0, null)) + Assert.Throws(() => Encoder.Decode(T0, null)) }) // -------------------------------------------------------- // Mapped @@ -27,14 +28,14 @@ describe('value/transform/Constructor', () => { .Decode((value) => 1) .Encode((value) => class {}) it('Should decode mapped', () => { - const R = Value.Decode(T1, class {}) + const R = Encoder.Decode(T1, class {}) Assert.IsEqual(R, 1) }) it('Should encode mapped', () => { - const R = Value.Encode(T1, null) + const R = Encoder.Encode(T1, null) Assert.IsTypeOf(R, 'function') }) it('Should throw on mapped decode', () => { - Assert.Throws(() => Value.Decode(T1, null)) + Assert.Throws(() => Encoder.Decode(T1, null)) }) }) diff --git a/test/runtime/value/transform/date.ts b/test/runtime/value/transform/date.ts index 1d83713e0..bc7fec5f5 100644 --- a/test/runtime/value/transform/date.ts +++ b/test/runtime/value/transform/date.ts @@ -1,3 +1,4 @@ +import * as Encoder from './_encoder' import { Assert } from '../../assert' import { Value } from '@sinclair/typebox/value' import { Type } from '@sinclair/typebox' @@ -10,15 +11,15 @@ describe('value/transform/Date', () => { .Decode((value) => value) .Encode((value) => value) it('Should decode identity', () => { - const R = Value.Decode(T0, new Date(123)) + const R = Encoder.Decode(T0, new Date(123)) Assert.IsEqual(R, new Date(123)) }) it('Should encode identity', () => { - const R = Value.Encode(T0, new Date(123)) + const R = Encoder.Encode(T0, new Date(123)) Assert.IsEqual(R, new Date(123)) }) it('Should throw on identity decode', () => { - Assert.Throws(() => Value.Decode(T0, null)) + Assert.Throws(() => Encoder.Decode(T0, null)) }) // -------------------------------------------------------- // Mapped @@ -27,14 +28,14 @@ describe('value/transform/Date', () => { .Decode((value) => 1) .Encode((value) => new Date()) it('Should decode mapped', () => { - const R = Value.Decode(T1, new Date()) + const R = Encoder.Decode(T1, new Date()) Assert.IsEqual(R, 1) }) it('Should encode mapped', () => { - const R = Value.Encode(T1, null) + const R = Encoder.Encode(T1, null) Assert.IsInstanceOf(R, Date) }) it('Should throw on mapped decode', () => { - Assert.Throws(() => Value.Decode(T1, null)) + Assert.Throws(() => Encoder.Decode(T1, null)) }) }) diff --git a/test/runtime/value/transform/enum.ts b/test/runtime/value/transform/enum.ts index 1df8f7793..b681ae33b 100644 --- a/test/runtime/value/transform/enum.ts +++ b/test/runtime/value/transform/enum.ts @@ -1,3 +1,4 @@ +import * as Encoder from './_encoder' import { Assert } from '../../assert' import { Value } from '@sinclair/typebox/value' import { Type } from '@sinclair/typebox' @@ -15,15 +16,15 @@ describe('value/transform/Enum', () => { .Decode((value) => value) .Encode((value) => value) it('Should decode identity', () => { - const R = Value.Decode(T0, E.A) + const R = Encoder.Decode(T0, E.A) Assert.IsEqual(R, E.A) }) it('Should encode identity', () => { - const R = Value.Encode(T0, E.A) + const R = Encoder.Encode(T0, E.A) Assert.IsEqual(R, E.A) }) it('Should throw on identity decode', () => { - Assert.Throws(() => Value.Decode(T0, null)) + Assert.Throws(() => Encoder.Decode(T0, null)) }) // -------------------------------------------------------- // Mapped @@ -32,14 +33,14 @@ describe('value/transform/Enum', () => { .Decode((value) => 1) .Encode((value) => E.A) it('Should decode mapped', () => { - const R = Value.Decode(T1, E.A) + const R = Encoder.Decode(T1, E.A) Assert.IsEqual(R, 1) }) it('Should encode mapped', () => { - const R = Value.Encode(T1, null) + const R = Encoder.Encode(T1, null) Assert.IsEqual(R, E.A) }) it('Should throw on mapped decode', () => { - Assert.Throws(() => Value.Decode(T1, null)) + Assert.Throws(() => Encoder.Decode(T1, null)) }) }) diff --git a/test/runtime/value/transform/function.ts b/test/runtime/value/transform/function.ts index 5d94b8d72..4bd89abc6 100644 --- a/test/runtime/value/transform/function.ts +++ b/test/runtime/value/transform/function.ts @@ -1,3 +1,4 @@ +import * as Encoder from './_encoder' import { Assert } from '../../assert' import { Value } from '@sinclair/typebox/value' import { Type } from '@sinclair/typebox' @@ -10,15 +11,15 @@ describe('value/transform/Function', () => { .Decode((value) => value) .Encode((value) => value) it('Should decode identity', () => { - const R = Value.Decode(T0, class {}) + const R = Encoder.Decode(T0, class {}) Assert.IsTypeOf(R, 'function') }) it('Should encode identity', () => { - const R = Value.Encode(T0, class {}) + const R = Encoder.Encode(T0, class {}) Assert.IsTypeOf(R, 'function') }) it('Should throw on identity decode', () => { - Assert.Throws(() => Value.Decode(T0, null)) + Assert.Throws(() => Encoder.Decode(T0, null)) }) // -------------------------------------------------------- // Mapped @@ -27,14 +28,14 @@ describe('value/transform/Function', () => { .Decode((value) => 1) .Encode((value) => function () {}) it('Should decode mapped', () => { - const R = Value.Decode(T1, function () {}) + const R = Encoder.Decode(T1, function () {}) Assert.IsEqual(R, 1) }) it('Should encode mapped', () => { - const R = Value.Encode(T1, null) + const R = Encoder.Encode(T1, null) Assert.IsTypeOf(R, 'function') }) it('Should throw on mapped decode', () => { - Assert.Throws(() => Value.Decode(T1, null)) + Assert.Throws(() => Encoder.Decode(T1, null)) }) }) diff --git a/test/runtime/value/transform/integer.ts b/test/runtime/value/transform/integer.ts index 2f91f3f35..099439aff 100644 --- a/test/runtime/value/transform/integer.ts +++ b/test/runtime/value/transform/integer.ts @@ -1,3 +1,4 @@ +import * as Encoder from './_encoder' import { Assert } from '../../assert' import { Value } from '@sinclair/typebox/value' import { Type } from '@sinclair/typebox' @@ -10,15 +11,15 @@ describe('value/transform/Integer', () => { .Decode((value) => value) .Encode((value) => value) it('Should decode identity', () => { - const R = Value.Decode(T0, 42) + const R = Encoder.Decode(T0, 42) Assert.IsEqual(R, 42) }) it('Should encode identity', () => { - const R = Value.Encode(T0, 42) + const R = Encoder.Encode(T0, 42) Assert.IsEqual(R, 42) }) it('Should throw on identity decode', () => { - Assert.Throws(() => Value.Decode(T0, null)) + Assert.Throws(() => Encoder.Decode(T0, null)) }) // -------------------------------------------------------- // Mapped @@ -27,14 +28,14 @@ describe('value/transform/Integer', () => { .Decode((value) => 1) .Encode((value) => 2) it('Should decode mapped', () => { - const R = Value.Decode(T1, 1) + const R = Encoder.Decode(T1, 1) Assert.IsEqual(R, 1) }) it('Should encode mapped', () => { - const R = Value.Encode(T1, null) + const R = Encoder.Encode(T1, null) Assert.IsEqual(R, 2) }) it('Should throw on mapped decode', () => { - Assert.Throws(() => Value.Decode(T1, null)) + Assert.Throws(() => Encoder.Decode(T1, null)) }) }) diff --git a/test/runtime/value/transform/intersect.ts b/test/runtime/value/transform/intersect.ts index 342f93abe..0d7f39043 100644 --- a/test/runtime/value/transform/intersect.ts +++ b/test/runtime/value/transform/intersect.ts @@ -1,3 +1,4 @@ +import * as Encoder from './_encoder' import { Assert } from '../../assert' import { Value } from '@sinclair/typebox/value' import { Type } from '@sinclair/typebox' @@ -14,15 +15,15 @@ describe('value/transform/Intersect', () => { .Decode(value => value) .Encode(value => value) it('Should decode identity', () => { - const R = Value.Decode(T0, { x: 1, y: 2 }) + const R = Encoder.Decode(T0, { x: 1, y: 2 }) Assert.IsEqual(R, { x: 1, y: 2 }) }) it('Should encode identity', () => { - const R = Value.Encode(T0, { x: 1, y: 2 }) + const R = Encoder.Encode(T0, { x: 1, y: 2 }) Assert.IsEqual(R, { x: 1, y: 2 }) }) it('Should throw on identity decode', () => { - Assert.Throws(() => Value.Decode(T0, null)) + Assert.Throws(() => Encoder.Decode(T0, null)) }) // -------------------------------------------------------- // Mapped @@ -35,15 +36,15 @@ describe('value/transform/Intersect', () => { .Decode((value) => 1) .Encode((value) => ({ x: 1, y: 2 })) it('Should decode mapped', () => { - const R = Value.Decode(T1, { x: 1, y: 2 }) + const R = Encoder.Decode(T1, { x: 1, y: 2 }) Assert.IsEqual(R, 1) }) it('Should encode mapped', () => { - const R = Value.Encode(T1, null) + const R = Encoder.Encode(T1, null) Assert.IsEqual(R, { x: 1, y: 2 }) }) it('Should throw on mapped decode', () => { - Assert.Throws(() => Value.Decode(T1, null)) + Assert.Throws(() => Encoder.Decode(T1, null)) }) // -------------------------------------------------------- // Mapped Property @@ -59,15 +60,15 @@ describe('value/transform/Intersect', () => { .Decode((value) => value) .Encode((value) => value) it('Should decode property', () => { - const R = Value.Decode(T2, { x: 1, y: 2 }) + const R = Encoder.Decode(T2, { x: 1, y: 2 }) Assert.IsEqual(R, { x: 1, y: '2' }) }) it('Should encode property', () => { - const R = Value.Encode(T2, { x: 1, y: '2' }) + const R = Encoder.Encode(T2, { x: 1, y: '2' }) Assert.IsEqual(R, { x: 1, y: 2 }) }) it('Should throw on property decode', () => { - Assert.Throws(() => Value.Decode(T2, null)) + Assert.Throws(() => Encoder.Decode(T2, null)) }) // -------------------------------------------------------- // Unevaluated Property @@ -83,15 +84,15 @@ describe('value/transform/Intersect', () => { unevaluatedProperties: N3 }) it('Should decode unevaluated property', () => { - const R = Value.Decode(T3, { x: 1, y: 2, z: 3 }) + const R = Encoder.Decode(T3, { x: 1, y: 2, z: 3 }) Assert.IsEqual(R, { x: 1, y: 2, z: '3' }) }) it('Should encode unevaluated property', () => { - const R = Value.Encode(T3, { x: 1, y: 2, z: '3' }) + const R = Encoder.Encode(T3, { x: 1, y: 2, z: '3' }) Assert.IsEqual(R, { x: 1, y: 2, z: 3 }) }) it('Should throw on unevaluated property decode', () => { - Assert.Throws(() => Value.Decode(T3, null)) + Assert.Throws(() => Encoder.Decode(T3, null)) }) // -------------------------------------------------------- // Transform Intersection Interior (Not Supported) @@ -109,14 +110,14 @@ describe('value/transform/Intersect', () => { .Decode((value) => value + 1) .Encode((value) => value - 1) it('Should decode exterior value type', () => { - const R = Value.Decode(T4, 1) + const R = Encoder.Decode(T4, 1) Assert.IsEqual(R, 2) }) it('Should encode exterior value type', () => { - const R = Value.Encode(T4, 2) + const R = Encoder.Encode(T4, 2) Assert.IsEqual(R, 1) }) it('Should throw on exterior value type decode', () => { - Assert.Throws(() => Value.Decode(T4, null)) + Assert.Throws(() => Encoder.Decode(T4, null)) }) }) diff --git a/test/runtime/value/transform/iterator.ts b/test/runtime/value/transform/iterator.ts index 90bc03773..66188acf1 100644 --- a/test/runtime/value/transform/iterator.ts +++ b/test/runtime/value/transform/iterator.ts @@ -1,3 +1,4 @@ +import * as Encoder from './_encoder' import { Assert } from '../../assert' import { Value } from '@sinclair/typebox/value' import { Type } from '@sinclair/typebox' @@ -10,15 +11,15 @@ describe('value/transform/Iterator', () => { .Decode((value) => value) .Encode((value) => value) it('Should decode identity', () => { - const R = Value.Decode(T0, (function* (): any {})()) + const R = Encoder.Decode(T0, (function* (): any {})()) Assert.IsTrue(Symbol.iterator in R) }) it('Should encode identity', () => { - const R = Value.Encode(T0, (function* (): any {})()) + const R = Encoder.Encode(T0, (function* (): any {})()) Assert.IsTrue(Symbol.iterator in R) }) it('Should throw on identity decode', () => { - Assert.Throws(() => Value.Decode(T0, null)) + Assert.Throws(() => Encoder.Decode(T0, null)) }) // -------------------------------------------------------- // Mapped @@ -27,14 +28,14 @@ describe('value/transform/Iterator', () => { .Decode((value) => 1) .Encode((value) => (function* (): any {})()) it('Should decode mapped', () => { - const R = Value.Decode(T1, (function* (): any {})()) + const R = Encoder.Decode(T1, (function* (): any {})()) Assert.IsEqual(R, 1) }) it('Should encode mapped', () => { - const R = Value.Encode(T1, null) + const R = Encoder.Encode(T1, null) Assert.IsTrue(Symbol.iterator in R) }) it('Should throw on mapped decode', () => { - Assert.Throws(() => Value.Decode(T1, null)) + Assert.Throws(() => Encoder.Decode(T1, null)) }) }) diff --git a/test/runtime/value/transform/literal.ts b/test/runtime/value/transform/literal.ts index 9e0f9e41c..ef88502bb 100644 --- a/test/runtime/value/transform/literal.ts +++ b/test/runtime/value/transform/literal.ts @@ -1,3 +1,4 @@ +import * as Encoder from './_encoder' import { Assert } from '../../assert' import { Value } from '@sinclair/typebox/value' import { Type } from '@sinclair/typebox' @@ -10,11 +11,11 @@ describe('value/transform/Literal', () => { .Decode((value) => value) .Encode((value) => value) it('Should decode mapped', () => { - const R = Value.Decode(T0, 123) + const R = Encoder.Decode(T0, 123) Assert.IsEqual(R, 123) }) it('Should encode mapped', () => { - const R = Value.Encode(T0, 123) + const R = Encoder.Encode(T0, 123) Assert.IsEqual(R, 123) }) // ----------------------------------------------- @@ -24,15 +25,15 @@ describe('value/transform/Literal', () => { .Decode((value) => 1) .Encode((value) => 'hello' as const) it('Should decode literal string', () => { - const R = Value.Decode(T1, 'hello') + const R = Encoder.Decode(T1, 'hello') Assert.IsEqual(R, 1) }) it('Should encode literal string', () => { - const R = Value.Encode(T1, null) + const R = Encoder.Encode(T1, null) Assert.IsEqual(R, 'hello') }) it('Should throw on decode literal string', () => { - Assert.Throws(() => Value.Decode(T1, null)) + Assert.Throws(() => Encoder.Decode(T1, null)) }) // ----------------------------------------------- // LiteralNumber @@ -41,15 +42,15 @@ describe('value/transform/Literal', () => { .Decode((value) => 1) .Encode((value) => 2 as const) it('Should decode literal number', () => { - const R = Value.Decode(T2, 2) + const R = Encoder.Decode(T2, 2) Assert.IsEqual(R, 1) }) it('Should encode literal number', () => { - const R = Value.Encode(T2, null) + const R = Encoder.Encode(T2, null) Assert.IsEqual(R, 2) }) it('Should throw on decode literal number', () => { - Assert.Throws(() => Value.Decode(T2, null)) + Assert.Throws(() => Encoder.Decode(T2, null)) }) // ----------------------------------------------- // LiteralBoolean @@ -58,14 +59,14 @@ describe('value/transform/Literal', () => { .Decode((value) => 1) .Encode((value) => true as const) it('Should decode literal boolean', () => { - const R = Value.Decode(T3, true) + const R = Encoder.Decode(T3, true) Assert.IsEqual(R, 1) }) it('Should encode literal boolean', () => { - const R = Value.Encode(T3, null) + const R = Encoder.Encode(T3, null) Assert.IsEqual(R, true) }) it('Should throw on decode literal boolean', () => { - Assert.Throws(() => Value.Decode(T3, null)) + Assert.Throws(() => Encoder.Decode(T3, null)) }) }) diff --git a/test/runtime/value/transform/never.ts b/test/runtime/value/transform/never.ts index 3a5414821..97e03478a 100644 --- a/test/runtime/value/transform/never.ts +++ b/test/runtime/value/transform/never.ts @@ -1,3 +1,4 @@ +import * as Encoder from './_encoder' import { Assert } from '../../assert' import { Value } from '@sinclair/typebox/value' import { Type } from '@sinclair/typebox' @@ -11,10 +12,10 @@ describe('value/transform/Never', () => { // @ts-ignore .Encode((value) => value) it('Should throw on identity encode', () => { - Assert.Throws(() => Value.Encode(T0, undefined)) + Assert.Throws(() => Encoder.Encode(T0, undefined)) }) it('Should throw on identity decode', () => { - Assert.Throws(() => Value.Decode(T0, undefined)) + Assert.Throws(() => Encoder.Decode(T0, undefined)) }) // -------------------------------------------------------- // Mapped @@ -25,9 +26,9 @@ describe('value/transform/Never', () => { // @ts-ignore .Encode((value) => 1) it('Should throw on mapped encode', () => { - Assert.Throws(() => Value.Encode(T1, undefined)) + Assert.Throws(() => Encoder.Encode(T1, undefined)) }) it('Should throw on mapped decode', () => { - Assert.Throws(() => Value.Decode(T1, undefined)) + Assert.Throws(() => Encoder.Decode(T1, undefined)) }) }) diff --git a/test/runtime/value/transform/not.ts b/test/runtime/value/transform/not.ts index 97277223e..68387ee58 100644 --- a/test/runtime/value/transform/not.ts +++ b/test/runtime/value/transform/not.ts @@ -1,3 +1,4 @@ +import * as Encoder from './_encoder' import { Assert } from '../../assert' import { Value } from '@sinclair/typebox/value' import { Type } from '@sinclair/typebox' @@ -10,15 +11,15 @@ describe('value/transform/Not', () => { .Decode((value) => value) .Encode((value) => value) it('Should decode identity', () => { - const R = Value.Decode(T0, 1) + const R = Encoder.Decode(T0, 1) Assert.IsEqual(R, 1) }) it('Should encode identity', () => { - const R = Value.Encode(T0, 1) + const R = Encoder.Encode(T0, 1) Assert.IsEqual(R, 1) }) it('Should throw on identity decode', () => { - Assert.Throws(() => Value.Decode(T0, '')) + Assert.Throws(() => Encoder.Decode(T0, '')) }) // -------------------------------------------------------- // Mapped @@ -27,14 +28,14 @@ describe('value/transform/Not', () => { .Decode((value) => 1) .Encode((value) => 2) it('Should decode mapped', () => { - const R = Value.Decode(T1, null) + const R = Encoder.Decode(T1, null) Assert.IsEqual(R, 1) }) it('Should encode mapped', () => { - const R = Value.Encode(T1, null) + const R = Encoder.Encode(T1, null) Assert.IsEqual(R, 2) }) it('Should throw on decode not', () => { - Assert.Throws(() => Value.Decode(T1, 'hello')) + Assert.Throws(() => Encoder.Decode(T1, 'hello')) }) }) diff --git a/test/runtime/value/transform/null.ts b/test/runtime/value/transform/null.ts index a113d1566..0cf90dc05 100644 --- a/test/runtime/value/transform/null.ts +++ b/test/runtime/value/transform/null.ts @@ -1,3 +1,4 @@ +import * as Encoder from './_encoder' import { Assert } from '../../assert' import { Value } from '@sinclair/typebox/value' import { Type } from '@sinclair/typebox' @@ -10,15 +11,15 @@ describe('value/transform/Null', () => { .Decode((value) => value) .Encode((value) => value) it('Should decode identity', () => { - const R = Value.Decode(T0, null) + const R = Encoder.Decode(T0, null) Assert.IsEqual(R, null) }) it('Should encode identity', () => { - const R = Value.Encode(T0, null) + const R = Encoder.Encode(T0, null) Assert.IsEqual(R, null) }) it('Should throw on identity decode', () => { - Assert.Throws(() => Value.Decode(T0, undefined)) + Assert.Throws(() => Encoder.Decode(T0, undefined)) }) // -------------------------------------------------------- // Mapped @@ -27,14 +28,14 @@ describe('value/transform/Null', () => { .Decode((value) => 1) .Encode((value) => null) it('Should decode mapped', () => { - const R = Value.Decode(T1, null) + const R = Encoder.Decode(T1, null) Assert.IsEqual(R, 1) }) it('Should encode mapped', () => { - const R = Value.Encode(T1, 123) + const R = Encoder.Encode(T1, 123) Assert.IsEqual(R, null) }) it('Should throw on mapped decode', () => { - Assert.Throws(() => Value.Decode(T1, undefined)) + Assert.Throws(() => Encoder.Decode(T1, undefined)) }) }) diff --git a/test/runtime/value/transform/number.ts b/test/runtime/value/transform/number.ts index dcd9328c7..2f06b50ff 100644 --- a/test/runtime/value/transform/number.ts +++ b/test/runtime/value/transform/number.ts @@ -1,3 +1,4 @@ +import * as Encoder from './_encoder' import { Assert } from '../../assert' import { Value } from '@sinclair/typebox/value' import { Type } from '@sinclair/typebox' @@ -10,15 +11,15 @@ describe('value/transform/Number', () => { .Decode((value) => value) .Encode((value) => value) it('Should decode identity', () => { - const R = Value.Decode(T0, 42) + const R = Encoder.Decode(T0, 42) Assert.IsEqual(R, 42) }) it('Should encode identity', () => { - const R = Value.Encode(T0, 42) + const R = Encoder.Encode(T0, 42) Assert.IsEqual(R, 42) }) it('Should throw on identity decode', () => { - Assert.Throws(() => Value.Decode(T0, null)) + Assert.Throws(() => Encoder.Decode(T0, null)) }) // -------------------------------------------------------- // Mapped @@ -27,14 +28,14 @@ describe('value/transform/Number', () => { .Decode((value) => value + 1) .Encode((value) => value - 1) it('Should decode mapped', () => { - const R = Value.Decode(T1, 1) + const R = Encoder.Decode(T1, 1) Assert.IsEqual(R, 2) }) it('Should encode mapped', () => { - const R = Value.Encode(T1, 2) + const R = Encoder.Encode(T1, 2) Assert.IsEqual(R, 1) }) it('Should throw on mapped decode', () => { - Assert.Throws(() => Value.Decode(T1, undefined)) + Assert.Throws(() => Encoder.Decode(T1, undefined)) }) }) diff --git a/test/runtime/value/transform/object.ts b/test/runtime/value/transform/object.ts index 2c3c0b423..0a73a998e 100644 --- a/test/runtime/value/transform/object.ts +++ b/test/runtime/value/transform/object.ts @@ -1,3 +1,4 @@ +import * as Encoder from './_encoder' import { Assert } from '../../assert' import { Value } from '@sinclair/typebox/value' import { Type } from '@sinclair/typebox' @@ -15,15 +16,15 @@ describe('value/transform/Object', () => { .Decode((value) => value) .Encode((value) => value) it('Should decode identity', () => { - const R = Value.Decode(T0, { x: 1, y: 2 }) + const R = Encoder.Decode(T0, { x: 1, y: 2 }) Assert.IsEqual(R, { x: 1, y: 2 }) }) it('Should encode identity', () => { - const R = Value.Encode(T0, { x: 1, y: 2 }) + const R = Encoder.Encode(T0, { x: 1, y: 2 }) Assert.IsEqual(R, { x: 1, y: 2 }) }) it('Should throw on identity decode', () => { - Assert.Throws(() => Value.Decode(T0, undefined)) + Assert.Throws(() => Encoder.Decode(T0, undefined)) }) // ---------------------------------------------------------- // Object @@ -37,15 +38,15 @@ describe('value/transform/Object', () => { .Decode((value) => 42) .Encode((value) => ({ x: 1, y: 2 })) it('Should decode mapped', () => { - const R = Value.Decode(T1, { x: 1, y: 2 }) + const R = Encoder.Decode(T1, { x: 1, y: 2 }) Assert.IsEqual(R, 42) }) it('Should encode mapped', () => { - const R = Value.Encode(T1, null) + const R = Encoder.Encode(T1, null) Assert.IsEqual(R, { x: 1, y: 2 }) }) it('Should throw on mapped decode', () => { - Assert.Throws(() => Value.Decode(T1, undefined)) + Assert.Throws(() => Encoder.Decode(T1, undefined)) }) // ---------------------------------------------------------- // Object: Transform Property @@ -58,15 +59,15 @@ describe('value/transform/Object', () => { y: N2, }) it('Should decode transform property', () => { - const R = Value.Decode(T2, { x: 1, y: 2 }) + const R = Encoder.Decode(T2, { x: 1, y: 2 }) Assert.IsEqual(R, { x: '1', y: '2' }) }) it('Should encode transform property', () => { - const R = Value.Encode(T2, { x: '1', y: '2' }) + const R = Encoder.Encode(T2, { x: '1', y: '2' }) Assert.IsEqual(R, { x: 1, y: 2 }) }) it('Should throw on decode transform property', () => { - Assert.Throws(() => Value.Decode(T2, undefined)) + Assert.Throws(() => Encoder.Decode(T2, undefined)) }) // ---------------------------------------------------------- // Object: Transform Property Nested (Twizzle) @@ -83,15 +84,15 @@ describe('value/transform/Object', () => { .Decode((value) => ({ x: value.y, y: value.x })) .Encode((value) => ({ x: value.y, y: value.x })) it('Should decode transform property nested', () => { - const R = Value.Decode(T3, { x: 1, y: 2 }) + const R = Encoder.Decode(T3, { x: 1, y: 2 }) Assert.IsEqual(R, { x: '2', y: '1' }) }) it('Should encode transform property nested', () => { - const R = Value.Encode(T3, { x: '1', y: '2' }) + const R = Encoder.Encode(T3, { x: '1', y: '2' }) Assert.IsEqual(R, { x: 2, y: 1 }) }) it('Should throw on decode transform property nested', () => { - Assert.Throws(() => Value.Decode(T3, undefined)) + Assert.Throws(() => Encoder.Decode(T3, undefined)) }) // ---------------------------------------------------------- // Object Additional Properties @@ -112,18 +113,18 @@ describe('value/transform/Object', () => { .Decode((value) => value) .Encode((value) => value) it('Should decode additional property', () => { - const R = Value.Decode(T4, { x: 1, y: 2 }) + const R = Encoder.Decode(T4, { x: 1, y: 2 }) Assert.IsEqual(R, { x: 1, y: '2' }) }) it('Should encode additional property', () => { - const R = Value.Encode(T4, { x: 1, y: '5' }) + const R = Encoder.Encode(T4, { x: 1, y: '5' }) Assert.IsEqual(R, { x: 1, y: 5 }) }) it('Should throw on additional property 1', () => { - Assert.Throws(() => Value.Decode(T4, undefined)) + Assert.Throws(() => Encoder.Decode(T4, undefined)) }) it('Should throw on additional property 2', () => { - Assert.Throws(() => Value.Decode(T4, { x: 1, y: true })) + Assert.Throws(() => Encoder.Decode(T4, { x: 1, y: true })) }) // ------------------------------------------------------------ // Map @@ -132,13 +133,13 @@ describe('value/transform/Object', () => { .Decode((value) => new Map(Object.entries(value))) .Encode((value) => Object.fromEntries(value.entries()) as any) it('should decode map', () => { - const R = Value.Decode(T5, { x: 'hello', y: 'world' }) + const R = Encoder.Decode(T5, { x: 'hello', y: 'world' }) Assert.IsInstanceOf(R, Map) Assert.IsEqual(R.get('x'), 'hello') Assert.IsEqual(R.get('y'), 'world') }) it('should encode map', () => { - const R = Value.Encode( + const R = Encoder.Encode( T5, new Map([ ['x', 'hello'], @@ -148,6 +149,6 @@ describe('value/transform/Object', () => { Assert.IsEqual(R, { x: 'hello', y: 'world' }) }) it('Should throw on map decode', () => { - Assert.Throws(() => Value.Decode(T5, {})) + Assert.Throws(() => Encoder.Decode(T5, {})) }) }) diff --git a/test/runtime/value/transform/promise.ts b/test/runtime/value/transform/promise.ts index 9a4f33c45..991bf5822 100644 --- a/test/runtime/value/transform/promise.ts +++ b/test/runtime/value/transform/promise.ts @@ -1,3 +1,4 @@ +import * as Encoder from './_encoder' import { Assert } from '../../assert' import { Value } from '@sinclair/typebox/value' import { Type } from '@sinclair/typebox' @@ -10,15 +11,15 @@ describe('value/transform/Promise', () => { .Decode((value) => value) .Encode((value) => value) it('Should decode identity', () => { - const R = Value.Decode(T0, Promise.resolve(1)) + const R = Encoder.Decode(T0, Promise.resolve(1)) Assert.IsTrue(R instanceof Promise) }) it('Should encode identity', () => { - const R = Value.Encode(T0, Promise.resolve(1)) + const R = Encoder.Encode(T0, Promise.resolve(1)) Assert.IsTrue(R instanceof Promise) }) it('Should throw on identity decode', () => { - Assert.Throws(() => Value.Decode(T0, undefined)) + Assert.Throws(() => Encoder.Decode(T0, undefined)) }) // -------------------------------------------------------- // Mapped @@ -27,14 +28,14 @@ describe('value/transform/Promise', () => { .Decode((value) => 1) .Encode((value) => Promise.resolve(1)) it('Should decode mapped', () => { - const R = Value.Decode(T, Promise.resolve(1)) + const R = Encoder.Decode(T, Promise.resolve(1)) Assert.IsEqual(R, 1) }) it('Should encode mapped', () => { - const R = Value.Encode(T, null) + const R = Encoder.Encode(T, null) Assert.IsTrue(R instanceof Promise) }) it('Should throw on mapped decode', () => { - Assert.Throws(() => Value.Decode(T, null)) + Assert.Throws(() => Encoder.Decode(T, null)) }) }) diff --git a/test/runtime/value/transform/record.ts b/test/runtime/value/transform/record.ts index 2b2bc3d82..a91172dbf 100644 --- a/test/runtime/value/transform/record.ts +++ b/test/runtime/value/transform/record.ts @@ -1,3 +1,4 @@ +import * as Encoder from './_encoder' import { Assert } from '../../assert' import { Value } from '@sinclair/typebox/value' import { Type } from '@sinclair/typebox' @@ -10,7 +11,7 @@ describe('value/transform/Record', () => { .Decode((value) => value) .Encode((value) => value) it('Should decode identity', () => { - const R = Value.Decode(T0, { + const R = Encoder.Decode(T0, { a: true, b: false, }) @@ -20,7 +21,7 @@ describe('value/transform/Record', () => { }) }) it('Should encode identity', () => { - const R = Value.Encode(T0, { + const R = Encoder.Encode(T0, { a: true, b: false, }) @@ -30,7 +31,7 @@ describe('value/transform/Record', () => { }) }) it('Should throw on identity', () => { - Assert.Throws(() => Value.Decode(T0, null)) + Assert.Throws(() => Encoder.Decode(T0, null)) }) // ------------------------------------------------------------ // Additional Properties True @@ -40,7 +41,7 @@ describe('value/transform/Record', () => { .Encode((value) => parseFloat(value.replace(/number-/g, ''))) const T1 = Type.Record(Type.Number(), N1) it('Should decode additional properties allowed', () => { - const R = Value.Decode(T1, { + const R = Encoder.Decode(T1, { 0: 1, a: true, }) @@ -50,7 +51,7 @@ describe('value/transform/Record', () => { }) }) it('Should encode additional properties allowed', () => { - const R = Value.Encode(T1, { + const R = Encoder.Encode(T1, { 0: 'number-1', a: true, }) @@ -60,7 +61,7 @@ describe('value/transform/Record', () => { }) }) it('Should throw on additional properties allowed', () => { - Assert.Throws(() => Value.Decode(T1, null)) + Assert.Throws(() => Encoder.Decode(T1, null)) }) // ------------------------------------------------------------ // Complex Transform @@ -73,7 +74,7 @@ describe('value/transform/Record', () => { .Encode((value) => (value === 'TRUE' ? true : false)) const T3 = Type.Record(Type.Number(), N2, { additionalProperties: B2 }) it('Should decode complex', () => { - const R = Value.Decode(T3, { + const R = Encoder.Decode(T3, { 0: 1, a: true, }) @@ -83,7 +84,7 @@ describe('value/transform/Record', () => { }) }) it('Should encode complex', () => { - const R = Value.Encode(T3, { + const R = Encoder.Encode(T3, { 0: 'number-1', a: 'TRUE', }) @@ -93,7 +94,7 @@ describe('value/transform/Record', () => { }) }) it('Should throw on complex decode', () => { - Assert.Throws(() => Value.Decode(T3, null)) + Assert.Throws(() => Encoder.Decode(T3, null)) }) // ------------------------------------------------------------ // Map @@ -102,13 +103,13 @@ describe('value/transform/Record', () => { .Decode((value) => new Map(Object.entries(value))) .Encode((value) => Object.fromEntries(value.entries())) it('should decode map', () => { - const R = Value.Decode(T4, { x: 'hello', y: 'world' }) + const R = Encoder.Decode(T4, { x: 'hello', y: 'world' }) Assert.IsInstanceOf(R, Map) Assert.IsEqual(R.get('x'), 'hello') Assert.IsEqual(R.get('y'), 'world') }) it('should encode map', () => { - const R = Value.Encode( + const R = Encoder.Encode( T4, new Map([ ['x', 'hello'], @@ -118,6 +119,6 @@ describe('value/transform/Record', () => { Assert.IsEqual(R, { x: 'hello', y: 'world' }) }) it('Should throw on map decode', () => { - Assert.Throws(() => Value.Decode(T4, null)) + Assert.Throws(() => Encoder.Decode(T4, null)) }) }) diff --git a/test/runtime/value/transform/recursive.ts b/test/runtime/value/transform/recursive.ts index 2b22d42e1..abea62630 100644 --- a/test/runtime/value/transform/recursive.ts +++ b/test/runtime/value/transform/recursive.ts @@ -1,3 +1,4 @@ +import * as Encoder from './_encoder' import { Assert } from '../../assert' import { Value } from '@sinclair/typebox/value' import { Type } from '@sinclair/typebox' @@ -17,7 +18,7 @@ describe('value/transform/Recursive', () => { .Decode((value) => value) .Encode((value) => value) it('Should decode identity', () => { - const R = Value.Decode(T0, { + const R = Encoder.Decode(T0, { value: 1, nodes: [ { value: 2, nodes: [] }, @@ -33,7 +34,7 @@ describe('value/transform/Recursive', () => { }) }) it('Should encode identity', () => { - const R = Value.Encode(T0, { + const R = Encoder.Encode(T0, { value: 1, nodes: [ { value: 2, nodes: [] }, @@ -49,7 +50,7 @@ describe('value/transform/Recursive', () => { }) }) it('Should throw on identity decode', () => { - Assert.Throws(() => Value.Decode(T0, undefined)) + Assert.Throws(() => Encoder.Decode(T0, undefined)) }) // ----------------------------------------------- // Mapped @@ -65,7 +66,7 @@ describe('value/transform/Recursive', () => { .Decode((value) => 1) .Encode((value) => ({ value: 1, nodes: [] })) it('Should decode mapped', () => { - const R = Value.Decode(T1, { + const R = Encoder.Decode(T1, { value: 1, nodes: [ { value: 2, nodes: [] }, @@ -75,11 +76,11 @@ describe('value/transform/Recursive', () => { Assert.IsEqual(R, 1) }) it('Should encode mapped', () => { - const R = Value.Encode(T1, null) + const R = Encoder.Encode(T1, null) Assert.IsEqual(R, { value: 1, nodes: [] }) }) it('Should throw on mapped decode', () => { - Assert.Throws(() => Value.Decode(T1, null)) + Assert.Throws(() => Encoder.Decode(T1, null)) }) // ----------------------------------------------- // Recursive Property Remap @@ -94,7 +95,7 @@ describe('value/transform/Recursive', () => { }), ) it('Should decode property', () => { - const R = Value.Decode(T2, { + const R = Encoder.Decode(T2, { value: 1, nodes: [ { value: 2, nodes: [] }, @@ -110,7 +111,7 @@ describe('value/transform/Recursive', () => { }) }) it('Should encode property', () => { - const R = Value.Encode(T2, { + const R = Encoder.Encode(T2, { value: new Date(1), nodes: [ { value: new Date(2), nodes: [] }, @@ -126,6 +127,6 @@ describe('value/transform/Recursive', () => { }) }) it('Should throw on decode property', () => { - Assert.Throws(() => Value.Decode(T2, null)) + Assert.Throws(() => Encoder.Decode(T2, null)) }) }) diff --git a/test/runtime/value/transform/ref.ts b/test/runtime/value/transform/ref.ts index 20385f9bb..92d252c29 100644 --- a/test/runtime/value/transform/ref.ts +++ b/test/runtime/value/transform/ref.ts @@ -1,3 +1,4 @@ +import * as Encoder from './_encoder' import { Assert } from '../../assert' import { Value } from '@sinclair/typebox/value' import { Type } from '@sinclair/typebox' @@ -11,15 +12,15 @@ describe('value/transform/Ref', () => { .Decode((value) => value) .Encode((value) => value) it('Should decode mapped', () => { - const R = Value.Decode(T0, [N0], 0) + const R = Encoder.Decode(T0, [N0], 0) Assert.IsEqual(R, 0) }) it('Should encode mapped', () => { - const R = Value.Encode(T0, [N0], 0) + const R = Encoder.Encode(T0, [N0], 0) Assert.IsEqual(R, 0) }) it('Should throw on identity decode', () => { - Assert.Throws(() => Value.Decode(T0, null)) + Assert.Throws(() => Encoder.Decode(T0, null)) }) // -------------------------------------------------------- // Mapped @@ -29,15 +30,15 @@ describe('value/transform/Ref', () => { .Decode((value) => value + 1) .Encode((value) => value - 1) it('Should decode mapped', () => { - const R = Value.Decode(T1, [N1], 0) + const R = Encoder.Decode(T1, [N1], 0) Assert.IsEqual(R, 1) }) it('Should encode mapped', () => { - const R = Value.Encode(T1, [N1], 1) + const R = Encoder.Encode(T1, [N1], 1) Assert.IsEqual(R, 0) }) it('Should throw on mapped decode', () => { - Assert.Throws(() => Value.Decode(T1, null)) + Assert.Throws(() => Encoder.Decode(T1, null)) }) // -------------------------------------------------------- // Mapped Remote @@ -49,14 +50,14 @@ describe('value/transform/Ref', () => { .Decode((value) => value + 1) .Encode((value) => value - 1) it('Should decode mapped remote', () => { - const R = Value.Decode(T2, [N2], 0) + const R = Encoder.Decode(T2, [N2], 0) Assert.IsEqual(R, 2) }) it('Should encode mapped remote', () => { - const R = Value.Encode(T2, [N2], 2) + const R = Encoder.Encode(T2, [N2], 2) Assert.IsEqual(R, 0) }) it('Should throw on mapped remote decode', () => { - Assert.Throws(() => Value.Decode(T1, null)) + Assert.Throws(() => Encoder.Decode(T1, null)) }) }) diff --git a/test/runtime/value/transform/string.ts b/test/runtime/value/transform/string.ts index 6c15c074f..3419c8691 100644 --- a/test/runtime/value/transform/string.ts +++ b/test/runtime/value/transform/string.ts @@ -1,3 +1,4 @@ +import * as Encoder from './_encoder' import { Assert } from '../../assert' import { Value } from '@sinclair/typebox/value' import { Type } from '@sinclair/typebox' @@ -6,20 +7,20 @@ describe('value/transform/String', () => { // -------------------------------------------------------- // Identity // -------------------------------------------------------- - const T0 = Type.Transform(Type.String()) - .Decode((value) => value) - .Encode((value) => value) - it('Should decode identity', () => { - const R = Value.Decode(T0, 'hello') - Assert.IsEqual(R, 'hello') - }) - it('Should encode identity', () => { - const R = Value.Encode(T0, 'hello') - Assert.IsEqual(R, 'hello') - }) - it('Should throw on identity decode', () => { - Assert.Throws(() => Value.Decode(T0, null)) - }) + // const T0 = Type.Transform(Type.String()) + // .Decode((value) => value) + // .Encode((value) => value) + // it('Should decode identity', () => { + // const R = Encoder.Decode(T0, 'hello') + // Assert.IsEqual(R, 'hello') + // }) + // it('Should encode identity', () => { + // const R = Encoder.Encode(T0, 'hello') + // Assert.IsEqual(R, 'hello') + // }) + // it('Should throw on identity decode', () => { + // Assert.Throws(() => Encoder.Decode(T0, null)) + // }) // -------------------------------------------------------- // Mapped // -------------------------------------------------------- @@ -27,14 +28,14 @@ describe('value/transform/String', () => { .Decode((value) => value.split('').reverse().join('')) .Encode((value) => value.split('').reverse().join('')) it('Should decode mapped', () => { - const R = Value.Decode(T1, 'ABC') + const R = Encoder.Decode(T1, 'ABC') Assert.IsEqual(R, 'CBA') }) it('Should encode mapped', () => { - const R = Value.Encode(T1, 'CBA') + const R = Encoder.Encode(T1, 'CBA') Assert.IsEqual(R, 'ABC') }) it('Should throw on mapped decode', () => { - Assert.Throws(() => Value.Decode(T1, null)) + Assert.Throws(() => Encoder.Decode(T1, null)) }) }) diff --git a/test/runtime/value/transform/symbol.ts b/test/runtime/value/transform/symbol.ts index 5dfca318d..a04a9d661 100644 --- a/test/runtime/value/transform/symbol.ts +++ b/test/runtime/value/transform/symbol.ts @@ -1,3 +1,4 @@ +import * as Encoder from './_encoder' import { Assert } from '../../assert' import { Value } from '@sinclair/typebox/value' import { Type } from '@sinclair/typebox' @@ -10,15 +11,15 @@ describe('value/transform/String', () => { .Decode((value) => value) .Encode((value) => value) it('Should decode identity', () => { - const R = Value.Decode(T0, Symbol('hello')) + const R = Encoder.Decode(T0, Symbol('hello')) Assert.IsEqual(R.description, 'hello') }) it('Should encode identity', () => { - const R = Value.Encode(T0, Symbol('hello')) + const R = Encoder.Encode(T0, Symbol('hello')) Assert.IsEqual(R.description, 'hello') }) it('Should throw on identity decode', () => { - Assert.Throws(() => Value.Decode(T0, null)) + Assert.Throws(() => Encoder.Decode(T0, null)) }) // -------------------------------------------------------- // Mapped @@ -27,14 +28,14 @@ describe('value/transform/String', () => { .Decode((value) => Symbol(value.description?.split('').reverse().join(''))) .Encode((value) => Symbol(value.description?.split('').reverse().join(''))) it('Should decode mapped', () => { - const R = Value.Decode(T1, Symbol('ABC')) + const R = Encoder.Decode(T1, Symbol('ABC')) Assert.IsEqual(R.description, 'CBA') }) it('Should encode mapped', () => { - const R = Value.Encode(T1, Symbol('CBA')) + const R = Encoder.Encode(T1, Symbol('CBA')) Assert.IsEqual(R.description, 'ABC') }) it('Should throw on mapped decode', () => { - Assert.Throws(() => Value.Decode(T1, null)) + Assert.Throws(() => Encoder.Decode(T1, null)) }) }) diff --git a/test/runtime/value/transform/template-literal.ts b/test/runtime/value/transform/template-literal.ts index 174abaa2b..3c8130a04 100644 --- a/test/runtime/value/transform/template-literal.ts +++ b/test/runtime/value/transform/template-literal.ts @@ -1,3 +1,4 @@ +import * as Encoder from './_encoder' import { Assert } from '../../assert' import { Value } from '@sinclair/typebox/value' import { Type } from '@sinclair/typebox' @@ -10,15 +11,15 @@ describe('value/transform/TemplateLiteral', () => { .Decode((value) => value) .Encode((value) => value) it('Should decode identity', () => { - const R = Value.Decode(T0, 'hello') + const R = Encoder.Decode(T0, 'hello') Assert.IsEqual(R, 'hello') }) it('Should encode identity', () => { - const R = Value.Encode(T0, 'hello') + const R = Encoder.Encode(T0, 'hello') Assert.IsEqual(R, 'hello') }) it('Should throw on identity decode', () => { - Assert.Throws(() => Value.Decode(T0, null)) + Assert.Throws(() => Encoder.Decode(T0, null)) }) // -------------------------------------------------------- // Mapped @@ -27,14 +28,14 @@ describe('value/transform/TemplateLiteral', () => { .Decode((value) => value.split('').reverse().join('') as 'CBA') .Encode((value) => value.split('').reverse().join('') as 'ABC') it('Should decode mapped', () => { - const R = Value.Decode(T1, 'ABC') + const R = Encoder.Decode(T1, 'ABC') Assert.IsEqual(R, 'CBA') }) it('Should encode mapped', () => { - const R = Value.Encode(T1, 'CBA') + const R = Encoder.Encode(T1, 'CBA') Assert.IsEqual(R, 'ABC') }) it('Should throw on mapped decode', () => { - Assert.Throws(() => Value.Decode(T1, null)) + Assert.Throws(() => Encoder.Decode(T1, null)) }) }) diff --git a/test/runtime/value/transform/tuple.ts b/test/runtime/value/transform/tuple.ts index cf6b9d51f..4b48377bc 100644 --- a/test/runtime/value/transform/tuple.ts +++ b/test/runtime/value/transform/tuple.ts @@ -1,3 +1,4 @@ +import * as Encoder from './_encoder' import { Assert } from '../../assert' import { Value } from '@sinclair/typebox/value' import { Type } from '@sinclair/typebox' @@ -10,15 +11,15 @@ describe('value/transform/Tuple', () => { .Decode((value) => value) .Encode((value) => value) it('Should decode identity', () => { - const R = Value.Decode(T0, [1, 2]) + const R = Encoder.Decode(T0, [1, 2]) Assert.IsEqual(R, [1, 2]) }) it('Should encode identity', () => { - const R = Value.Encode(T0, [1, 2]) + const R = Encoder.Encode(T0, [1, 2]) Assert.IsEqual(R, [1, 2]) }) it('Should throw on identity decode', () => { - Assert.Throws(() => Value.Decode(T0, null)) + Assert.Throws(() => Encoder.Decode(T0, null)) }) // -------------------------------------------------------- // Mapped @@ -27,15 +28,15 @@ describe('value/transform/Tuple', () => { .Decode((value) => [value[0] + 1] as [number]) .Encode((value) => [value[0] - 1] as [number]) it('Should decode mapped', () => { - const R = Value.Decode(T1, [0]) + const R = Encoder.Decode(T1, [0]) Assert.IsEqual(R, [1]) }) it('Should encode mapped', () => { - const R = Value.Encode(T1, [1]) + const R = Encoder.Encode(T1, [1]) Assert.IsEqual(R, [0]) }) it('Should throw on mapped decode', () => { - Assert.Throws(() => Value.Decode(T1, null)) + Assert.Throws(() => Encoder.Decode(T1, null)) }) // -------------------------------------------------------- // Mapped Element @@ -47,15 +48,15 @@ describe('value/transform/Tuple', () => { .Decode((value) => value) .Encode((value) => value) it('Should decode mapped element', () => { - const R = Value.Decode(T2, [0]) + const R = Encoder.Decode(T2, [0]) Assert.IsEqual(R, [1]) }) it('Should encode mapped element', () => { - const R = Value.Encode(T2, [1]) + const R = Encoder.Encode(T2, [1]) Assert.IsEqual(R, [0]) }) it('Should throw on mapped element decode', () => { - Assert.Throws(() => Value.Decode(T2, null)) + Assert.Throws(() => Encoder.Decode(T2, null)) }) // -------------------------------------------------------- // Mapped Element @@ -67,15 +68,15 @@ describe('value/transform/Tuple', () => { .Decode((value) => [value[0].toString()]) .Encode((value) => [parseFloat(value[0])] as [number]) it('Should decode mapped element', () => { - const R = Value.Decode(T3, [0]) + const R = Encoder.Decode(T3, [0]) Assert.IsEqual(R, ['1']) }) it('Should encode mapped element', () => { - const R = Value.Encode(T3, ['1']) + const R = Encoder.Encode(T3, ['1']) Assert.IsEqual(R, [0]) }) it('Should throw on mapped element decode', () => { - Assert.Throws(() => Value.Decode(T3, null)) + Assert.Throws(() => Encoder.Decode(T3, null)) }) // ------------------------------------------------------------ // Set @@ -84,15 +85,15 @@ describe('value/transform/Tuple', () => { .Decode((value) => new Set(value)) .Encode((value) => [...value] as any) it('should decode set', () => { - const R = Value.Decode(T4, [1]) + const R = Encoder.Decode(T4, [1]) Assert.IsInstanceOf(R, Set) Assert.IsTrue(R.has(1)) }) it('should encode set', () => { - const R = Value.Encode(T4, new Set([1])) + const R = Encoder.Encode(T4, new Set([1])) Assert.IsEqual(R, [1]) }) it('Should throw on set decode', () => { - Assert.Throws(() => Value.Decode(T4, {})) + Assert.Throws(() => Encoder.Decode(T4, {})) }) }) diff --git a/test/runtime/value/transform/undefined.ts b/test/runtime/value/transform/undefined.ts index 7f8582a9e..60c5a4c01 100644 --- a/test/runtime/value/transform/undefined.ts +++ b/test/runtime/value/transform/undefined.ts @@ -1,3 +1,4 @@ +import * as Encoder from './_encoder' import { Assert } from '../../assert' import { Value } from '@sinclair/typebox/value' import { Type } from '@sinclair/typebox' @@ -10,15 +11,15 @@ describe('value/transform/Undefined', () => { .Decode((value) => value) .Encode((value) => value) it('Should decode identity', () => { - const R = Value.Decode(T0, undefined) + const R = Encoder.Decode(T0, undefined) Assert.IsEqual(R, undefined) }) it('Should encode identity', () => { - const R = Value.Encode(T0, undefined) + const R = Encoder.Encode(T0, undefined) Assert.IsEqual(R, undefined) }) it('Should throw on identity decode', () => { - Assert.Throws(() => Value.Decode(T0, null)) + Assert.Throws(() => Encoder.Decode(T0, null)) }) // -------------------------------------------------------- // Mapped @@ -27,14 +28,14 @@ describe('value/transform/Undefined', () => { .Decode((value) => null) .Encode((value) => undefined) it('Should decode mapped', () => { - const R = Value.Decode(T1, undefined) + const R = Encoder.Decode(T1, undefined) Assert.IsEqual(R, null) }) it('Should encode mapped', () => { - const R = Value.Encode(T1, null) + const R = Encoder.Encode(T1, null) Assert.IsEqual(R, undefined) }) it('Should throw on mapped decode', () => { - Assert.Throws(() => Value.Decode(T1, null)) + Assert.Throws(() => Encoder.Decode(T1, null)) }) }) diff --git a/test/runtime/value/transform/union.ts b/test/runtime/value/transform/union.ts index d1a9cebc5..93eeff81c 100644 --- a/test/runtime/value/transform/union.ts +++ b/test/runtime/value/transform/union.ts @@ -1,3 +1,4 @@ +import * as Encoder from './_encoder' import { Assert } from '../../assert' import { Value } from '@sinclair/typebox/value' import { Type, TSchema } from '@sinclair/typebox' @@ -14,15 +15,15 @@ describe('value/transform/Union', () => { .Decode((value) => value) .Encode((value) => value) it('Should decode identity', () => { - const R = Value.Decode(T0, { x: 1 }) + const R = Encoder.Decode(T0, { x: 1 }) Assert.IsEqual(R, { x: 1 }) }) it('Should encode identity', () => { - const R = Value.Encode(T0, { y: 2 }) + const R = Encoder.Encode(T0, { y: 2 }) Assert.IsEqual(R, { y: 2 }) }) it('Should throw on identity decode', () => { - Assert.Throws(() => Value.Decode(T0, null)) + Assert.Throws(() => Encoder.Decode(T0, null)) }) // -------------------------------------------------------- // Mapped @@ -35,15 +36,15 @@ describe('value/transform/Union', () => { .Decode((value) => 'test' as const) .Encode((value) => ({ type: 'hello' as const })) it('Should decode mapped', () => { - const R = Value.Decode(T1, { type: 'hello' }) + const R = Encoder.Decode(T1, { type: 'hello' }) Assert.IsEqual(R, 'test') }) it('Should encode mapped', () => { - const R = Value.Encode(T1, 'test') + const R = Encoder.Encode(T1, 'test') Assert.IsEqual(R, { type: 'hello' }) }) it('Should throw on mapped decode', () => { - Assert.Throws(() => Value.Decode(T1, null)) + Assert.Throws(() => Encoder.Decode(T1, null)) }) // -------------------------------------------------------- // Mapped ValueType @@ -62,23 +63,23 @@ describe('value/transform/Union', () => { return value }) it('Should decode value type 1', () => { - const R = Value.Decode(T2, 0) + const R = Encoder.Decode(T2, 0) Assert.IsEqual(R, 2) }) it('Should decode value type 2', () => { - const R = Value.Decode(T2, 'hello') + const R = Encoder.Decode(T2, 'hello') Assert.IsEqual(R, 'hello') }) it('Should encode value type 1', () => { - const R = Value.Encode(T2, 'hello') + const R = Encoder.Encode(T2, 'hello') Assert.IsEqual(R, 'world') }) it('Should encode value type 2', () => { - const R = Value.Encode(T2, 2) + const R = Encoder.Encode(T2, 2) Assert.IsEqual(R, 0) }) it('Should throw on value type decode', () => { - Assert.Throws(() => Value.Decode(T2, null)) + Assert.Throws(() => Encoder.Decode(T2, null)) }) // -------------------------------------------------------- // Mapped ObjectType @@ -102,23 +103,23 @@ describe('value/transform/Union', () => { .Decode((value) => value) .Encode((value) => value) it('Should decode object types 1', () => { - const R = Value.Decode(T3, { x: 0 }) + const R = Encoder.Decode(T3, { x: 0 }) Assert.IsEqual(R, { x: 1 }) }) it('Should decode object types 2', () => { - const R = Value.Decode(T3, { x: 'abc' }) + const R = Encoder.Decode(T3, { x: 'abc' }) Assert.IsEqual(R, { x: 'cba' }) }) it('Should encode object types 1', () => { - const R = Value.Encode(T3, { x: 1 }) + const R = Encoder.Encode(T3, { x: 1 }) Assert.IsEqual(R, { x: 0 }) }) it('Should encode object types 2', () => { - const R = Value.Encode(T3, { x: 'cba' }) + const R = Encoder.Encode(T3, { x: 'cba' }) Assert.IsEqual(R, { x: 'abc' }) }) it('Should throw on object types decode', () => { - Assert.Throws(() => Value.Decode(T3, null)) + Assert.Throws(() => Encoder.Decode(T3, null)) }) // -------------------------------------------------------- // Mapped Mixed Types @@ -140,31 +141,31 @@ describe('value/transform/Union', () => { .Decode((value) => typeof value === 'number' ? value + 1 : value) .Encode((value) => typeof value === 'number' ? value - 1 : value) it('Should decode mixed types 1', () => { - const R = Value.Decode(T4, { x: 0 }) + const R = Encoder.Decode(T4, { x: 0 }) Assert.IsEqual(R, { x: 1 }) }) it('Should decode mixed types 2', () => { - const R = Value.Decode(T4, 0) + const R = Encoder.Decode(T4, 0) Assert.IsEqual(R, 2) }) it('Should decode mixed types 3', () => { - const R = Value.Decode(T4, [0]) + const R = Encoder.Decode(T4, [0]) Assert.IsEqual(R, [1]) }) it('Should encode mixed types 1', () => { - const R = Value.Encode(T4, { x: 1 }) + const R = Encoder.Encode(T4, { x: 1 }) Assert.IsEqual(R, { x: 0 }) }) it('Should encode mixed types 2', () => { - const R = Value.Encode(T4, 2) + const R = Encoder.Encode(T4, 2) Assert.IsEqual(R, 0) }) it('Should encode mixed types 3', () => { - const R = Value.Encode(T4, [1]) + const R = Encoder.Encode(T4, [1]) Assert.IsEqual(R, [0]) }) it('Should throw on mixed types decode', () => { - Assert.Throws(() => Value.Decode(T4, null)) + Assert.Throws(() => Encoder.Decode(T4, null)) }) // -------------------------------------------------------- // Interior Union Transform @@ -176,26 +177,26 @@ describe('value/transform/Union', () => { .Encode((value) => value.toISOString()) const T52 = Type.Union([Type.Null(), T51]) it('Should decode interior union 1', () => { - const R = Value.Decode(T52, null) + const R = Encoder.Decode(T52, null) Assert.IsEqual(R, null) }) it('Should decode interior union 2', () => { - const R = Value.Decode(T52, new Date().toISOString()) + const R = Encoder.Decode(T52, new Date().toISOString()) Assert.IsInstanceOf(R, Date) }) it('Should encode interior union 1', () => { - const R = Value.Encode(T52, null) + const R = Encoder.Encode(T52, null) Assert.IsEqual(R, null) }) it('Should encode interior union 2', () => { const D = new Date() - const R = Value.Encode(T52, D) + const R = Encoder.Encode(T52, D) Assert.IsEqual(R, D.toISOString()) }) it('Should throw on interior union decode', () => { - Assert.Throws(() => Value.Decode(T52, {})) + Assert.Throws(() => Encoder.Decode(T52, {})) }) it('Should throw on interior union encode', () => { - Assert.Throws(() => Value.Encode(T52, 1)) + Assert.Throws(() => Encoder.Encode(T52, 1)) }) }) diff --git a/test/runtime/value/transform/unknown.ts b/test/runtime/value/transform/unknown.ts index 2fe673a17..463980d57 100644 --- a/test/runtime/value/transform/unknown.ts +++ b/test/runtime/value/transform/unknown.ts @@ -1,3 +1,4 @@ +import * as Encoder from './_encoder' import { Assert } from '../../assert' import { Value } from '@sinclair/typebox/value' import { Type } from '@sinclair/typebox' @@ -10,11 +11,11 @@ describe('value/transform/Unknown', () => { .Decode((value) => value) .Encode((value) => value) it('Should decode mapped', () => { - const R = Value.Decode(T0, 123) + const R = Encoder.Decode(T0, 123) Assert.IsEqual(R, 123) }) it('Should encode mapped', () => { - const R = Value.Encode(T0, 123) + const R = Encoder.Encode(T0, 123) Assert.IsEqual(R, 123) }) // -------------------------------------------------------- @@ -24,11 +25,11 @@ describe('value/transform/Unknown', () => { .Decode((value) => 1) .Encode((value) => 2) it('Should decode mapped', () => { - const R = Value.Decode(T1, null) + const R = Encoder.Decode(T1, null) Assert.IsEqual(R, 1) }) it('Should encode mapped', () => { - const R = Value.Encode(T1, null) + const R = Encoder.Encode(T1, null) Assert.IsEqual(R, 2) }) }) diff --git a/test/runtime/value/transform/unsafe.ts b/test/runtime/value/transform/unsafe.ts index 76d31cb4f..f679e01c9 100644 --- a/test/runtime/value/transform/unsafe.ts +++ b/test/runtime/value/transform/unsafe.ts @@ -1,3 +1,4 @@ +import * as Encoder from './_encoder' import { Assert } from '../../assert' import { Value } from '@sinclair/typebox/value' import { Type, Kind, TypeRegistry } from '@sinclair/typebox' @@ -16,15 +17,15 @@ describe('value/transform/Unsafe', () => { .Decode((value) => value) .Encode((value) => value) it('Should decode identity', () => { - const R = Value.Decode(T0, 'hello') + const R = Encoder.Decode(T0, 'hello') Assert.IsEqual(R, 'hello') }) it('Should encode identity', () => { - const R = Value.Encode(T0, 'hello') + const R = Encoder.Encode(T0, 'hello') Assert.IsEqual(R, 'hello') }) it('Should throw on identity decode', () => { - Assert.Throws(() => Value.Decode(T0, null)) + Assert.Throws(() => Encoder.Decode(T0, null)) }) // -------------------------------------------------------- // Mapped @@ -33,14 +34,14 @@ describe('value/transform/Unsafe', () => { .Decode((value) => value.split('').reverse().join('')) .Encode((value) => value.split('').reverse().join('')) it('Should decode mapped', () => { - const R = Value.Decode(T1, 'ABC') + const R = Encoder.Decode(T1, 'ABC') Assert.IsEqual(R, 'CBA') }) it('Should encode mapped', () => { - const R = Value.Encode(T1, 'CBA') + const R = Encoder.Encode(T1, 'CBA') Assert.IsEqual(R, 'ABC') }) it('Should throw on mapped decode', () => { - Assert.Throws(() => Value.Decode(T1, null)) + Assert.Throws(() => Encoder.Decode(T1, null)) }) })