diff --git a/examples/index.ts b/examples/index.ts index 3aba4b866..c660cbef8 100644 --- a/examples/index.ts +++ b/examples/index.ts @@ -6,6 +6,10 @@ import { Run } from './benchmark' // Todo: Investigate Union Default (initialize interior types) // Todo: Implement Value.Clean() Tests -const T = Type.Tuple([Type.Number(), Type.Number()]) -const R = Value.Clean(T, [1, 2, 3]) + +const X = Type.Object({ x: Type.Number() }) +const Y = Type.Object({ y: Type.Number() }) +const T = Type.Union([X, Y]) +const R = Value.Clean(T, { u: null, x: 1 }) + console.log(R) diff --git a/test/runtime/value/clean/index.ts b/test/runtime/value/clean/index.ts index 0343390fb..b290dd224 100644 --- a/test/runtime/value/clean/index.ts +++ b/test/runtime/value/clean/index.ts @@ -27,3 +27,8 @@ 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/clean/object.ts b/test/runtime/value/clean/object.ts index bfe5bce32..805665a7e 100644 --- a/test/runtime/value/clean/object.ts +++ b/test/runtime/value/clean/object.ts @@ -3,6 +3,9 @@ import { Type } from '@sinclair/typebox' import { Assert } from '../../assert/index' describe('value/clean/Object', () => { + // ---------------------------------------------------------------- + // Clean + // ---------------------------------------------------------------- it('Should clean 1', () => { const T = Type.Object({ x: Type.Number() }) const R = Value.Clean(T, null) diff --git a/test/runtime/value/clean/uint8array.ts b/test/runtime/value/clean/uint8array.ts new file mode 100644 index 000000000..732eb5b71 --- /dev/null +++ b/test/runtime/value/clean/uint8array.ts @@ -0,0 +1,11 @@ +import { Value } from '@sinclair/typebox/value' +import { Type } from '@sinclair/typebox' +import { Assert } from '../../assert/index' + +describe('value/clean/Uint8Array', () => { + it('Should clean 1', () => { + const T = Type.Uint8Array() + const R = Value.Clean(T, null) + Assert.IsEqual(R, null) + }) +}) diff --git a/test/runtime/value/clean/undefined.ts b/test/runtime/value/clean/undefined.ts new file mode 100644 index 000000000..4b590c41b --- /dev/null +++ b/test/runtime/value/clean/undefined.ts @@ -0,0 +1,11 @@ +import { Value } from '@sinclair/typebox/value' +import { Type } from '@sinclair/typebox' +import { Assert } from '../../assert/index' + +describe('value/clean/Undefined', () => { + it('Should clean 1', () => { + const T = Type.Undefined() + const R = Value.Clean(T, null) + Assert.IsEqual(R, null) + }) +}) diff --git a/test/runtime/value/clean/union.ts b/test/runtime/value/clean/union.ts new file mode 100644 index 000000000..443e3d651 --- /dev/null +++ b/test/runtime/value/clean/union.ts @@ -0,0 +1,134 @@ +import { Value } from '@sinclair/typebox/value' +import { Type } from '@sinclair/typebox' +import { Assert } from '../../assert/index' + +describe('value/clean/Union', () => { + // ---------------------------------------------------------------- + // Clean + // ---------------------------------------------------------------- + it('Should clean 1', () => { + const T = Type.Union([Type.Number(), Type.Boolean()]) + const R = Value.Clean(T, null) + Assert.IsEqual(R, null) + }) + it('Should clean 2', () => { + const T = Type.Union([Type.Number(), Type.Boolean()]) + const R = Value.Clean(T, 1) + Assert.IsEqual(R, 1) + }) + it('Should clean 2', () => { + const T = Type.Union([Type.Number(), Type.Boolean()]) + const R = Value.Clean(T, true) + Assert.IsEqual(R, true) + }) + // ---------------------------------------------------------------- + // Clean Select + // ---------------------------------------------------------------- + it('Should clean select 1', () => { + const X = Type.Object({ x: Type.Number() }) + const Y = Type.Object({ y: Type.Number() }) + const T = Type.Union([X, Y]) + const R = Value.Clean(T, null) + Assert.IsEqual(R, null) + }) + it('Should clean select 2', () => { + const X = Type.Object({ x: Type.Number() }) + const Y = Type.Object({ y: Type.Number() }) + const T = Type.Union([X, Y]) + const R = Value.Clean(T, {}) + Assert.IsEqual(R, {}) + }) + it('Should clean select 3', () => { + const X = Type.Object({ x: Type.Number() }) + const Y = Type.Object({ y: Type.Number() }) + const T = Type.Union([X, Y]) + const R = Value.Clean(T, { x: null }) + Assert.IsEqual(R, { x: null }) + }) + it('Should clean select 4', () => { + const X = Type.Object({ x: Type.Number() }) + const Y = Type.Object({ y: Type.Number() }) + const T = Type.Union([X, Y]) + const R = Value.Clean(T, { y: null }) + Assert.IsEqual(R, { y: null }) + }) + // ---------------------------------------------------------------- + // Clean Select Discard + // ---------------------------------------------------------------- + it('Should clean select discard 1', () => { + const X = Type.Object({ x: Type.Number() }) + const Y = Type.Object({ y: Type.Number() }) + const T = Type.Union([X, Y]) + const R = Value.Clean(T, null) + Assert.IsEqual(R, null) + }) + it('Should clean select discard 2', () => { + const X = Type.Object({ x: Type.Number() }) + const Y = Type.Object({ y: Type.Number() }) + const T = Type.Union([X, Y]) + const R = Value.Clean(T, { u: null }) + Assert.IsEqual(R, { u: null }) // no match + }) + it('Should clean select discard 3', () => { + const X = Type.Object({ x: Type.Number() }) + const Y = Type.Object({ y: Type.Number() }) + const T = Type.Union([X, Y]) + const R = Value.Clean(T, { u: null, x: 1 }) + Assert.IsEqual(R, { x: 1 }) + }) + it('Should clean select discard 4', () => { + const X = Type.Object({ x: Type.Number() }) + const Y = Type.Object({ y: Type.Number() }) + const T = Type.Union([X, Y]) + const R = Value.Clean(T, { u: null, y: 1 }) + Assert.IsEqual(R, { y: 1 }) + }) + // ---------------------------------------------------------------- + // Clean Select Retain + // ---------------------------------------------------------------- + it('Should clean select retain 1', () => { + const X = Type.Object({ x: Type.Number() }, { additionalProperties: Type.Null() }) + const Y = Type.Object({ y: Type.Number() }, { additionalProperties: Type.Null() }) + const T = Type.Union([X, Y]) + const R = Value.Clean(T, null) + Assert.IsEqual(R, null) + }) + it('Should clean select retain 2', () => { + const X = Type.Object({ x: Type.Number() }, { additionalProperties: Type.Null() }) + const Y = Type.Object({ y: Type.Number() }, { additionalProperties: Type.Null() }) + const T = Type.Union([X, Y]) + const R = Value.Clean(T, { u: null }) + Assert.IsEqual(R, { u: null }) + }) + it('Should clean select retain 3', () => { + const X = Type.Object({ x: Type.Number() }, { additionalProperties: Type.Null() }) + const Y = Type.Object({ y: Type.Number() }, { additionalProperties: Type.Null() }) + const T = Type.Union([X, Y]) + const R = Value.Clean(T, { u: null, x: 1 }) + Assert.IsEqual(R, { u: null, x: 1 }) + }) + it('Should clean select retain 4', () => { + const X = Type.Object({ x: Type.Number() }, { additionalProperties: Type.Null() }) + const Y = Type.Object({ y: Type.Number() }, { additionalProperties: Type.Null() }) + const T = Type.Union([X, Y]) + const R = Value.Clean(T, { u: null, y: 1 }) + Assert.IsEqual(R, { u: null, y: 1 }) + }) + // ---------------------------------------------------------------- + // Clean Select First and Discard + // ---------------------------------------------------------------- + it('Should clean select first and discard 1', () => { + const X = Type.Object({ x: Type.Number() }) + const Y = Type.Object({ y: Type.Number() }, { additionalProperties: Type.Null() }) + const T = Type.Union([X, Y]) + const R = Value.Clean(T, { u: null, x: 1 }) + Assert.IsEqual(R, { x: 1 }) + }) + it('Should clean select first and discard 2', () => { + const X = Type.Object({ x: Type.Number() }) + const Y = Type.Object({ y: Type.Number() }, { additionalProperties: Type.Null() }) + const T = Type.Union([X, Y]) + const R = Value.Clean(T, { u: null, y: 1 }) + Assert.IsEqual(R, { u: null, y: 1 }) + }) +}) diff --git a/test/runtime/value/clean/unknown.ts b/test/runtime/value/clean/unknown.ts new file mode 100644 index 000000000..edba3ebfc --- /dev/null +++ b/test/runtime/value/clean/unknown.ts @@ -0,0 +1,11 @@ +import { Value } from '@sinclair/typebox/value' +import { Type } from '@sinclair/typebox' +import { Assert } from '../../assert/index' + +describe('value/clean/Unknown', () => { + it('Should clean 1', () => { + const T = Type.Unknown() + const R = Value.Clean(T, null) + Assert.IsEqual(R, null) + }) +}) diff --git a/test/runtime/value/clean/void.ts b/test/runtime/value/clean/void.ts new file mode 100644 index 000000000..cd4c20db0 --- /dev/null +++ b/test/runtime/value/clean/void.ts @@ -0,0 +1,11 @@ +import { Value } from '@sinclair/typebox/value' +import { Type } from '@sinclair/typebox' +import { Assert } from '../../assert/index' + +describe('value/clean/Void', () => { + it('Should clean 1', () => { + const T = Type.Void() + const R = Value.Clean(T, null) + Assert.IsEqual(R, null) + }) +})