diff --git a/rename.sh b/rename.sh new file mode 100755 index 00000000..ac0c2444 --- /dev/null +++ b/rename.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +# Define the relative directory path +DIR="test/validation" + +# Find all .ts and .tsx files in the specified directory and rename them +find "$DIR" -type f \( -name "*.ts" -o -name "*.tsx" \) | while read -r file; do + # Construct the new file name with .test.ts extension + new_file="${file%.*}.test.ts" + + # Rename the file + mv "$file" "$new_file" + + echo "Renamed $file to $new_file" +done diff --git a/test/index.test.ts b/test/index.test.ts deleted file mode 100644 index bb990e2a..00000000 --- a/test/index.test.ts +++ /dev/null @@ -1,81 +0,0 @@ -import fs from 'fs' -import { pick } from 'lodash' -import { basename, extname, resolve } from 'path' -import { describe, expect, it } from 'vitest' -import { - assert as assertValue, - create as createValue, - StructError, -} from '../src' -describe('superstruct', () => { - describe('validation', () => { - const kindsDir = resolve(__dirname, 'validation') - const kinds = fs - .readdirSync(kindsDir) - .filter((t) => t[0] !== '.') - .map((t) => basename(t, extname(t))) - - for (const kind of kinds) { - describe(kind, async () => { - const testsDir = resolve(kindsDir, kind) - const tests = fs - .readdirSync(testsDir) - .filter((t) => t[0] !== '.') - .map((t) => basename(t, extname(t))) - - for (const name of tests) { - const module = await import(resolve(testsDir, name)) - const { Struct, data, create, only, skip, output, failures } = module - const run = only ? it.only : skip ? it.skip : it - run(name, () => { - let actual - let err - - try { - if (create) { - actual = createValue(data, Struct) - } else { - assertValue(data, Struct) - actual = data - } - } catch (e) { - if (!(e instanceof StructError)) { - throw e - } - - err = e - } - - if ('output' in module) { - if (err) { - throw new Error( - `Expected "${name}" fixture not to throw an error but it did:\n\n${err}` - ) - } - - expect(actual).toStrictEqual(output) - } else if ('failures' in module) { - if (!err) { - throw new Error( - `Expected "${name}" fixture to throw an error but it did not.` - ) - } - - const props = ['type', 'path', 'refinement', 'value', 'branch'] - const actualFailures = err - .failures() - .map((failure) => pick(failure, ...props)) - - expect(actualFailures).toStrictEqual(failures) - expect(pick(err, ...props)).toStrictEqual(failures[0]) - } else { - throw new Error( - `The "${name}" fixture did not define an \`output\` or \`failures\` export.` - ) - } - }) - } - }) - } - }) -}) diff --git a/test/matchers.ts b/test/matchers.ts new file mode 100644 index 00000000..3088c919 --- /dev/null +++ b/test/matchers.ts @@ -0,0 +1,60 @@ +import { isEqual, pick } from 'lodash' +import { expect } from 'vitest' +import { StructError } from '../src' + +const FILTERED_PROPS = ['type', 'path', 'refinement', 'value', 'branch'] + +expect.extend({ + toMatchStructError: (received: StructError | undefined, expected: any) => { + // Make sure the error exists + if (!(received instanceof StructError)) { + return { + message: () => `Expected error to be a StructError`, + pass: false, + actual: received, + expected, + } + } + + const actualFailures = received + .failures() + .map((failure) => pick(failure, ...FILTERED_PROPS)) + + // Check that the failures match + if (!isEqual(actualFailures, expected)) { + return { + message: () => `Expected error.failures to match expected`, + pass: false, + actual: actualFailures, + expected, + } + } + + const strippedError = pick(received, ...FILTERED_PROPS) + + // Check that the first failure properties are also the properties of the StructError + if (!isEqual(strippedError, expected[0])) { + return { + message: () => + `Expected error properties to match first expected failure`, + pass: false, + actual: strippedError, + expected, + } + } + + return { + message: () => `${received} matches ${expected}`, + pass: true, + } + }, +}) + +interface CustomMatchers { + toMatchStructError: (expected: any) => void +} + +declare module 'vitest' { + interface Assertion extends CustomMatchers {} + interface AsymmetricMatchersContaining extends CustomMatchers {} +} diff --git a/test/validation/any/valid-number.test.ts b/test/validation/any/valid-number.test.ts new file mode 100644 index 00000000..19342718 --- /dev/null +++ b/test/validation/any/valid-number.test.ts @@ -0,0 +1,9 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { any } from '../../../src' + +test('Valid any number', () => { + const data = 1 + assert(data, any()) + expect(data).toStrictEqual(1) +}) diff --git a/test/validation/any/valid-number.ts b/test/validation/any/valid-number.ts deleted file mode 100644 index 70af230d..00000000 --- a/test/validation/any/valid-number.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { any } from '../../../src' - -export const Struct = any() - -export const data = 1 - -export const output = 1 diff --git a/test/validation/any/valid-string.test.ts b/test/validation/any/valid-string.test.ts new file mode 100644 index 00000000..a2d48de4 --- /dev/null +++ b/test/validation/any/valid-string.test.ts @@ -0,0 +1,9 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { any } from '../../../src' + +test('Valid any string', () => { + const data = 'valid' + assert(data, any()) + expect(data).toStrictEqual('valid') +}) diff --git a/test/validation/any/valid-string.ts b/test/validation/any/valid-string.ts deleted file mode 100644 index 3ee3159d..00000000 --- a/test/validation/any/valid-string.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { any } from '../../../src' - -export const Struct = any() - -export const data = 'valid' - -export const output = 'valid' diff --git a/test/validation/any/valid-undefined.test.ts b/test/validation/any/valid-undefined.test.ts new file mode 100644 index 00000000..c594d764 --- /dev/null +++ b/test/validation/any/valid-undefined.test.ts @@ -0,0 +1,9 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { any } from '../../../src' + +test('Valid any undefined', () => { + const data = undefined + assert(data, any()) + expect(data).toStrictEqual(undefined) +}) diff --git a/test/validation/any/valid-undefined.ts b/test/validation/any/valid-undefined.ts deleted file mode 100644 index d22b8108..00000000 --- a/test/validation/any/valid-undefined.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { any } from '../../../src' - -export const Struct = any() - -export const data = undefined - -export const output = undefined diff --git a/test/validation/array/invalid-element-property.test.ts b/test/validation/array/invalid-element-property.test.ts new file mode 100644 index 00000000..8fc4ef17 --- /dev/null +++ b/test/validation/array/invalid-element-property.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { array, object, string } from '../../../src' + +test('Invalid array element property', () => { + const data = [{ id: '1' }, { id: false }, { id: '3' }] + const [err, res] = validate(data, array(object({ id: string() }))) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: false, + type: 'string', + refinement: undefined, + path: [1, 'id'], + branch: [data, data[1], data[1].id], + }, + ]) +}) diff --git a/test/validation/array/invalid-element-property.ts b/test/validation/array/invalid-element-property.ts deleted file mode 100644 index f7754163..00000000 --- a/test/validation/array/invalid-element-property.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { array, object, string } from '../../../src' - -export const Struct = array(object({ id: string() })) - -export const data = [{ id: '1' }, { id: false }, { id: '3' }] - -export const failures = [ - { - value: false, - type: 'string', - refinement: undefined, - path: [1, 'id'], - branch: [data, data[1], data[1].id], - }, -] diff --git a/test/validation/array/invalid-element.test.ts b/test/validation/array/invalid-element.test.ts new file mode 100644 index 00000000..654cea0b --- /dev/null +++ b/test/validation/array/invalid-element.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { array, number } from '../../../src' + +test('Invalid array element', () => { + const data = [1, 'invalid', 3] + const [err, res] = validate(data, array(number())) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 'invalid', + type: 'number', + refinement: undefined, + path: [1], + branch: [data, data[1]], + }, + ]) +}) diff --git a/test/validation/array/invalid-element.ts b/test/validation/array/invalid-element.ts deleted file mode 100644 index 86738987..00000000 --- a/test/validation/array/invalid-element.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { array, number } from '../../../src' - -export const Struct = array(number()) - -export const data = [1, 'invalid', 3] - -export const failures = [ - { - value: 'invalid', - type: 'number', - refinement: undefined, - path: [1], - branch: [data, data[1]], - }, -] diff --git a/test/validation/array/invalid-opaque.test.ts b/test/validation/array/invalid-opaque.test.ts new file mode 100644 index 00000000..ba93ed17 --- /dev/null +++ b/test/validation/array/invalid-opaque.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { array } from '../../../src' + +test('Invalid array opaque', () => { + const data = 'invalid' + const [err, res] = validate(data, array()) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 'invalid', + type: 'array', + refinement: undefined, + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/array/invalid-opaque.ts b/test/validation/array/invalid-opaque.ts deleted file mode 100644 index 21685f08..00000000 --- a/test/validation/array/invalid-opaque.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { array } from '../../../src' - -export const Struct = array() - -export const data = 'invalid' - -export const failures = [ - { - value: 'invalid', - type: 'array', - refinement: undefined, - path: [], - branch: [data], - }, -] diff --git a/test/validation/array/invalid.test.ts b/test/validation/array/invalid.test.ts new file mode 100644 index 00000000..a80827e1 --- /dev/null +++ b/test/validation/array/invalid.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { array, number } from '../../../src' + +test('Invalid array', () => { + const data = 'invalid' + const [err, res] = validate(data, array(number())) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 'invalid', + type: 'array', + refinement: undefined, + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/array/invalid.ts b/test/validation/array/invalid.ts deleted file mode 100644 index 5d1b040d..00000000 --- a/test/validation/array/invalid.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { array, number } from '../../../src' - -export const Struct = array(number()) - -export const data = 'invalid' - -export const failures = [ - { - value: 'invalid', - type: 'array', - refinement: undefined, - path: [], - branch: [data], - }, -] diff --git a/test/validation/array/valid-frozen.test.ts b/test/validation/array/valid-frozen.test.ts new file mode 100644 index 00000000..9e73b4ab --- /dev/null +++ b/test/validation/array/valid-frozen.test.ts @@ -0,0 +1,9 @@ +import { create } from '../../../src' +import { expect, test } from 'vitest' +import { array, number } from '../../../src' + +test('Valid array frozen', () => { + const data = Object.freeze([1, 2, 3]) + const res = create(data, array(number())) + expect(res).toStrictEqual([1, 2, 3]) +}) diff --git a/test/validation/array/valid-frozen.ts b/test/validation/array/valid-frozen.ts deleted file mode 100644 index 814291fe..00000000 --- a/test/validation/array/valid-frozen.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { array, number } from '../../../src' - -export const Struct = array(number()) - -export const data = Object.freeze([1, 2, 3]) - -export const output = [1, 2, 3] - -export const create = true diff --git a/test/validation/array/valid-opaque.test.ts b/test/validation/array/valid-opaque.test.ts new file mode 100644 index 00000000..545124af --- /dev/null +++ b/test/validation/array/valid-opaque.test.ts @@ -0,0 +1,9 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { array } from '../../../src' + +test('Valid array opaque', () => { + const data = [1, 'b', true] + assert(data, array()) + expect(data).toStrictEqual([1, 'b', true]) +}) diff --git a/test/validation/array/valid-opaque.ts b/test/validation/array/valid-opaque.ts deleted file mode 100644 index 21d50e56..00000000 --- a/test/validation/array/valid-opaque.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { array } from '../../../src' - -export const Struct = array() - -export const data = [1, 'b', true] - -export const output = [1, 'b', true] diff --git a/test/validation/array/valid.test.ts b/test/validation/array/valid.test.ts new file mode 100644 index 00000000..827d18c3 --- /dev/null +++ b/test/validation/array/valid.test.ts @@ -0,0 +1,9 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { array, number } from '../../../src' + +test('Valid array', () => { + const data = [1, 2, 3] + assert(data, array(number())) + expect(data).toStrictEqual([1, 2, 3]) +}) diff --git a/test/validation/array/valid.ts b/test/validation/array/valid.ts deleted file mode 100644 index a98de1cd..00000000 --- a/test/validation/array/valid.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { array, number } from '../../../src' - -export const Struct = array(number()) - -export const data = [1, 2, 3] - -export const output = [1, 2, 3] diff --git a/test/validation/assign/invalid-object.test.ts b/test/validation/assign/invalid-object.test.ts new file mode 100644 index 00000000..918ec025 --- /dev/null +++ b/test/validation/assign/invalid-object.test.ts @@ -0,0 +1,34 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { object, assign, string, number } from '../../../src' + +const A = object({ a: string() }) +const B = object({ a: number(), b: number() }) + +test('Invalid assign object', () => { + const data = { + a: 'invalid', + b: 2, + c: 5, + } + + const [err, res] = validate(data, assign(A, B)) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 'invalid', + type: 'number', + refinement: undefined, + path: ['a'], + branch: [data, data.a], + }, + { + branch: [data, data.c], + path: ['c'], + refinement: undefined, + type: 'never', + value: 5, + }, + ]) +}) diff --git a/test/validation/assign/invalid-object.ts b/test/validation/assign/invalid-object.ts deleted file mode 100644 index 4ac6af5d..00000000 --- a/test/validation/assign/invalid-object.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { object, assign, string, number } from '../../../src' - -const A = object({ a: string() }) -const B = object({ a: number(), b: number() }) - -export const Struct = assign(A, B) - -export const data = { - a: 'invalid', - b: 2, - c: 5, -} - -export const failures = [ - { - value: 'invalid', - type: 'number', - refinement: undefined, - path: ['a'], - branch: [data, data.a], - }, - { - branch: [data, data.c], - path: ['c'], - refinement: undefined, - type: 'never', - value: 5, - }, -] diff --git a/test/validation/assign/invalid-type.test.ts b/test/validation/assign/invalid-type.test.ts new file mode 100644 index 00000000..8be22b7f --- /dev/null +++ b/test/validation/assign/invalid-type.test.ts @@ -0,0 +1,27 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { type, object, assign, string, number } from '../../../src' + +const A = type({ a: string() }) +const B = object({ a: number(), b: number() }) + +test('Invalid assign type', () => { + const data = { + a: 'invalid', + b: 2, + c: 5, + } + + const [err, res] = validate(data, assign(A, B)) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 'invalid', + type: 'number', + refinement: undefined, + path: ['a'], + branch: [data, data.a], + }, + ]) +}) diff --git a/test/validation/assign/invalid-type.ts b/test/validation/assign/invalid-type.ts deleted file mode 100644 index 840e5e23..00000000 --- a/test/validation/assign/invalid-type.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { type, object, assign, string, number } from '../../../src' - -const A = type({ a: string() }) -const B = object({ a: number(), b: number() }) - -export const Struct = assign(A, B) - -export const data = { - a: 'invalid', - b: 2, - c: 5, -} - -export const failures = [ - { - value: 'invalid', - type: 'number', - refinement: undefined, - path: ['a'], - branch: [data, data.a], - }, -] diff --git a/test/validation/assign/valid-object.test.ts b/test/validation/assign/valid-object.test.ts new file mode 100644 index 00000000..17833c0e --- /dev/null +++ b/test/validation/assign/valid-object.test.ts @@ -0,0 +1,20 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { type, assign, string, number } from '../../../src' + +const A = type({ a: string() }) +const B = type({ a: number(), b: number() }) + +test('Valid assign object', () => { + const data = { + a: 1, + b: 2, + } + + assert(data, assign(A, B)) + + expect(data).toStrictEqual({ + a: 1, + b: 2, + }) +}) diff --git a/test/validation/assign/valid-object.ts b/test/validation/assign/valid-object.ts deleted file mode 100644 index 237c5638..00000000 --- a/test/validation/assign/valid-object.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { type, assign, string, number } from '../../../src' - -const A = type({ a: string() }) -const B = type({ a: number(), b: number() }) - -export const Struct = assign(A, B) - -export const data = { - a: 1, - b: 2, -} - -export const output = { - a: 1, - b: 2, -} diff --git a/test/validation/assign/valid-type.test.ts b/test/validation/assign/valid-type.test.ts new file mode 100644 index 00000000..af8a14d0 --- /dev/null +++ b/test/validation/assign/valid-type.test.ts @@ -0,0 +1,22 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { type, object, assign, string, number } from '../../../src' + +const A = type({ a: string() }) +const B = object({ b: number() }) + +test('Valid assign type', () => { + const data = { + a: '1', + b: 2, + c: 3, + } + + assert(data, assign(A, B)) + + expect(data).toStrictEqual({ + a: '1', + b: 2, + c: 3, + }) +}) diff --git a/test/validation/assign/valid-type.ts b/test/validation/assign/valid-type.ts deleted file mode 100644 index fe2e1473..00000000 --- a/test/validation/assign/valid-type.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { type, object, assign, string, number } from '../../../src' - -const A = type({ a: string() }) -const B = object({ b: number() }) - -export const Struct = assign(A, B) - -export const data = { - a: '1', - b: 2, - c: 3, -} - -export const output = { - a: '1', - b: 2, - c: 3, -} diff --git a/test/validation/bigint/invalid-number.test.ts b/test/validation/bigint/invalid-number.test.ts new file mode 100644 index 00000000..e9989404 --- /dev/null +++ b/test/validation/bigint/invalid-number.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { bigint } from '../../../src' + +test('Invalid bigint number', () => { + const data = 3 + const [err, res] = validate(data, bigint()) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 3, + type: 'bigint', + refinement: undefined, + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/bigint/invalid-number.ts b/test/validation/bigint/invalid-number.ts deleted file mode 100644 index dc0e2662..00000000 --- a/test/validation/bigint/invalid-number.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { bigint } from '../../../src' - -export const Struct = bigint() - -export const data = 3 - -export const failures = [ - { - value: 3, - type: 'bigint', - refinement: undefined, - path: [], - branch: [data], - }, -] diff --git a/test/validation/bigint/invalid.test.ts b/test/validation/bigint/invalid.test.ts new file mode 100644 index 00000000..2374c332 --- /dev/null +++ b/test/validation/bigint/invalid.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { bigint } from '../../../src' + +test('Invalid bigint', () => { + const data = 'invalid' + const [err, res] = validate(data, bigint()) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 'invalid', + type: 'bigint', + refinement: undefined, + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/bigint/invalid.ts b/test/validation/bigint/invalid.ts deleted file mode 100644 index bafb1785..00000000 --- a/test/validation/bigint/invalid.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { bigint } from '../../../src' - -export const Struct = bigint() - -export const data = 'invalid' - -export const failures = [ - { - value: 'invalid', - type: 'bigint', - refinement: undefined, - path: [], - branch: [data], - }, -] diff --git a/test/validation/bigint/valid.test.ts b/test/validation/bigint/valid.test.ts new file mode 100644 index 00000000..5eb16f40 --- /dev/null +++ b/test/validation/bigint/valid.test.ts @@ -0,0 +1,9 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { bigint } from '../../../src' + +test('Valid bigint', () => { + const data = 542n + assert(data, bigint()) + expect(data).toStrictEqual(542n) +}) diff --git a/test/validation/bigint/valid.ts b/test/validation/bigint/valid.ts deleted file mode 100644 index 4fcdfe60..00000000 --- a/test/validation/bigint/valid.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { bigint } from '../../../src' - -export const Struct = bigint() - -export const data = 542n - -export const output = 542n diff --git a/test/validation/boolean/invalid.test.ts b/test/validation/boolean/invalid.test.ts new file mode 100644 index 00000000..087f28b0 --- /dev/null +++ b/test/validation/boolean/invalid.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { boolean } from '../../../src' + +test('Invalid boolean', () => { + const data = 'invalid' + const [err, res] = validate(data, boolean()) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 'invalid', + type: 'boolean', + refinement: undefined, + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/boolean/invalid.ts b/test/validation/boolean/invalid.ts deleted file mode 100644 index 01b05be6..00000000 --- a/test/validation/boolean/invalid.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { boolean } from '../../../src' - -export const Struct = boolean() - -export const data = 'invalid' - -export const failures = [ - { - value: 'invalid', - type: 'boolean', - refinement: undefined, - path: [], - branch: [data], - }, -] diff --git a/test/validation/boolean/valid.test.ts b/test/validation/boolean/valid.test.ts new file mode 100644 index 00000000..b2d57559 --- /dev/null +++ b/test/validation/boolean/valid.test.ts @@ -0,0 +1,9 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { boolean } from '../../../src' + +test('Valid boolean', () => { + const data = true + assert(data, boolean()) + expect(data).toStrictEqual(true) +}) diff --git a/test/validation/boolean/valid.ts b/test/validation/boolean/valid.ts deleted file mode 100644 index 86d8c122..00000000 --- a/test/validation/boolean/valid.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { boolean } from '../../../src' - -export const Struct = boolean() - -export const data = true - -export const output = true diff --git a/test/validation/coerce/changed.test.ts b/test/validation/coerce/changed.test.ts new file mode 100644 index 00000000..b72c6e66 --- /dev/null +++ b/test/validation/coerce/changed.test.ts @@ -0,0 +1,14 @@ +import { create } from '../../../src' +import { expect, test } from 'vitest' +import { string, unknown, coerce } from '../../../src' + +test('Changed coerce', () => { + const data = null + + const res = create( + data, + coerce(string(), unknown(), (x) => (x == null ? 'unknown' : x)) + ) + + expect(res).toStrictEqual('unknown') +}) diff --git a/test/validation/coerce/changed.ts b/test/validation/coerce/changed.ts deleted file mode 100644 index da3c9cdd..00000000 --- a/test/validation/coerce/changed.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { string, unknown, coerce } from '../../../src' - -export const Struct = coerce(string(), unknown(), (x) => - x == null ? 'unknown' : x -) - -export const data = null - -export const output = 'unknown' - -export const create = true diff --git a/test/validation/coerce/condition-not-met.test.ts b/test/validation/coerce/condition-not-met.test.ts new file mode 100644 index 00000000..5a9e895a --- /dev/null +++ b/test/validation/coerce/condition-not-met.test.ts @@ -0,0 +1,27 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { string, number, coerce } from '../../../src' + +test('Condition coerce not met', () => { + const data = false + + const [err, res] = validate( + data, + coerce(string(), number(), (x) => 'known'), + { + coerce: true, + } + ) + + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: false, + type: 'string', + refinement: undefined, + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/coerce/condition-not-met.ts b/test/validation/coerce/condition-not-met.ts deleted file mode 100644 index 2f36b2bc..00000000 --- a/test/validation/coerce/condition-not-met.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { string, number, coerce } from '../../../src' - -export const Struct = coerce(string(), number(), (x) => 'known') - -export const data = false - -export const failures = [ - { - value: false, - type: 'string', - refinement: undefined, - path: [], - branch: [data], - }, -] - -export const create = true diff --git a/test/validation/coerce/unchanged.test.ts b/test/validation/coerce/unchanged.test.ts new file mode 100644 index 00000000..87ba2310 --- /dev/null +++ b/test/validation/coerce/unchanged.test.ts @@ -0,0 +1,14 @@ +import { create } from '../../../src' +import { expect, test } from 'vitest' +import { string, unknown, coerce } from '../../../src' + +test('Unchanged coerce', () => { + const data = 'known' + + const res = create( + data, + coerce(string(), unknown(), (x) => (x == null ? 'unknown' : x)) + ) + + expect(res).toStrictEqual('known') +}) diff --git a/test/validation/coerce/unchanged.ts b/test/validation/coerce/unchanged.ts deleted file mode 100644 index abe5aeba..00000000 --- a/test/validation/coerce/unchanged.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { string, unknown, coerce } from '../../../src' - -export const Struct = coerce(string(), unknown(), (x) => - x == null ? 'unknown' : x -) - -export const data = 'known' - -export const output = 'known' - -export const create = true diff --git a/test/validation/date/invalid-date.test.ts b/test/validation/date/invalid-date.test.ts new file mode 100644 index 00000000..68295012 --- /dev/null +++ b/test/validation/date/invalid-date.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { date } from '../../../src' + +test('Invalid date date', () => { + const data = new Date('invalid') + const [err, res] = validate(data, date()) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: data, + type: 'date', + refinement: undefined, + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/date/invalid-date.ts b/test/validation/date/invalid-date.ts deleted file mode 100644 index 3ffdef33..00000000 --- a/test/validation/date/invalid-date.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { date } from '../../../src' - -export const Struct = date() - -export const data = new Date('invalid') - -export const failures = [ - { - value: data, - type: 'date', - refinement: undefined, - path: [], - branch: [data], - }, -] diff --git a/test/validation/date/invalid.test.ts b/test/validation/date/invalid.test.ts new file mode 100644 index 00000000..07ebb114 --- /dev/null +++ b/test/validation/date/invalid.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { date } from '../../../src' + +test('Invalid date', () => { + const data = 'invalid' + const [err, res] = validate(data, date()) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 'invalid', + type: 'date', + refinement: undefined, + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/date/invalid.ts b/test/validation/date/invalid.ts deleted file mode 100644 index e4eca7c5..00000000 --- a/test/validation/date/invalid.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { date } from '../../../src' - -export const Struct = date() - -export const data = 'invalid' - -export const failures = [ - { - value: 'invalid', - type: 'date', - refinement: undefined, - path: [], - branch: [data], - }, -] diff --git a/test/validation/date/valid.test.ts b/test/validation/date/valid.test.ts new file mode 100644 index 00000000..625fad3a --- /dev/null +++ b/test/validation/date/valid.test.ts @@ -0,0 +1,9 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { date } from '../../../src' + +test('Valid date', () => { + const data = new Date(0) + assert(data, date()) + expect(data).toStrictEqual(new Date(0)) +}) diff --git a/test/validation/date/valid.ts b/test/validation/date/valid.ts deleted file mode 100644 index 947156a3..00000000 --- a/test/validation/date/valid.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { date } from '../../../src' - -export const Struct = date() - -export const data = new Date(0) - -export const output = new Date(0) diff --git a/test/validation/defaulted/function.test.ts b/test/validation/defaulted/function.test.ts new file mode 100644 index 00000000..a1892603 --- /dev/null +++ b/test/validation/defaulted/function.test.ts @@ -0,0 +1,12 @@ +import { create } from '../../../src' +import { expect, test } from 'vitest' +import { number, defaulted } from '../../../src' + +test('Function defaulted', () => { + const data = undefined + const res = create( + data, + defaulted(number(), () => 42) + ) + expect(res).toStrictEqual(42) +}) diff --git a/test/validation/defaulted/function.ts b/test/validation/defaulted/function.ts deleted file mode 100644 index aac1e68c..00000000 --- a/test/validation/defaulted/function.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { number, defaulted } from '../../../src' - -export const Struct = defaulted(number(), () => 42) - -export const data = undefined - -export const output = 42 - -export const create = true diff --git a/test/validation/defaulted/mixin.test.ts b/test/validation/defaulted/mixin.test.ts new file mode 100644 index 00000000..45cf0dfd --- /dev/null +++ b/test/validation/defaulted/mixin.test.ts @@ -0,0 +1,27 @@ +import { create } from '../../../src' +import { expect, test } from 'vitest' +import { defaulted, string, object, number } from '../../../src' + +test('Mixin defaulted', () => { + const data = { + version: 0, + } + + const res = create( + data, + defaulted( + object({ + title: string(), + version: number(), + }), + { + title: 'Untitled', + } + ) + ) + + expect(res).toStrictEqual({ + title: 'Untitled', + version: 0, + }) +}) diff --git a/test/validation/defaulted/mixin.ts b/test/validation/defaulted/mixin.ts deleted file mode 100644 index f587c2d6..00000000 --- a/test/validation/defaulted/mixin.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { defaulted, string, object, number } from '../../../src' - -export const Struct = defaulted( - object({ - title: string(), - version: number(), - }), - { - title: 'Untitled', - } -) - -export const data = { - version: 0, -} - -export const output = { - title: 'Untitled', - version: 0, -} - -export const create = true diff --git a/test/validation/defaulted/nested-double.test.ts b/test/validation/defaulted/nested-double.test.ts new file mode 100644 index 00000000..97d76fb0 --- /dev/null +++ b/test/validation/defaulted/nested-double.test.ts @@ -0,0 +1,25 @@ +import { create } from '../../../src' +import { expect, test } from 'vitest' +import { defaulted, string, object } from '../../../src' + +test('Nested defaulted double', () => { + const data = {} + + const res = create( + data, + object({ + book: defaulted( + object({ + title: defaulted(string(), 'Untitled'), + }), + {} + ), + }) + ) + + expect(res).toStrictEqual({ + book: { + title: 'Untitled', + }, + }) +}) diff --git a/test/validation/defaulted/nested-double.ts b/test/validation/defaulted/nested-double.ts deleted file mode 100644 index f661c285..00000000 --- a/test/validation/defaulted/nested-double.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { defaulted, string, object } from '../../../src' - -export const Struct = object({ - book: defaulted( - object({ - title: defaulted(string(), 'Untitled'), - }), - {} - ), -}) - -export const data = {} - -export const output = { - book: { - title: 'Untitled', - }, -} - -export const create = true diff --git a/test/validation/defaulted/nested.test.ts b/test/validation/defaulted/nested.test.ts new file mode 100644 index 00000000..7a93c7d8 --- /dev/null +++ b/test/validation/defaulted/nested.test.ts @@ -0,0 +1,18 @@ +import { create } from '../../../src' +import { expect, test } from 'vitest' +import { defaulted, string, object } from '../../../src' + +test('Nested defaulted', () => { + const data = {} + + const res = create( + data, + object({ + title: defaulted(string(), 'Untitled'), + }) + ) + + expect(res).toStrictEqual({ + title: 'Untitled', + }) +}) diff --git a/test/validation/defaulted/nested.ts b/test/validation/defaulted/nested.ts deleted file mode 100644 index b432ea84..00000000 --- a/test/validation/defaulted/nested.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { defaulted, string, object } from '../../../src' - -export const Struct = object({ - title: defaulted(string(), 'Untitled'), -}) - -export const data = {} - -export const output = { - title: 'Untitled', -} - -export const create = true diff --git a/test/validation/defaulted/strict.test.ts b/test/validation/defaulted/strict.test.ts new file mode 100644 index 00000000..e3f8a132 --- /dev/null +++ b/test/validation/defaulted/strict.test.ts @@ -0,0 +1,40 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { defaulted, string, type, number } from '../../../src' + +test('Strict defaulted', () => { + const data = { + version: 0, + } + + const [err, res] = validate( + data, + defaulted( + type({ + title: string(), + version: number(), + }), + { + title: 'Untitled', + }, + { + strict: true, + } + ), + { + coerce: true, + } + ) + + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: undefined, + type: 'string', + refinement: undefined, + path: ['title'], + branch: [data, undefined], + }, + ]) +}) diff --git a/test/validation/defaulted/strict.ts b/test/validation/defaulted/strict.ts deleted file mode 100644 index aa10f41a..00000000 --- a/test/validation/defaulted/strict.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { defaulted, string, type, number } from '../../../src' - -export const Struct = defaulted( - type({ - title: string(), - version: number(), - }), - { - title: 'Untitled', - }, - { - strict: true, - } -) - -export const data = { - version: 0, -} - -export const failures = [ - { - value: undefined, - type: 'string', - refinement: undefined, - path: ['title'], - branch: [data, undefined], - }, -] - -export const create = true diff --git a/test/validation/defaulted/value.test.ts b/test/validation/defaulted/value.test.ts new file mode 100644 index 00000000..3d3df041 --- /dev/null +++ b/test/validation/defaulted/value.test.ts @@ -0,0 +1,9 @@ +import { create } from '../../../src' +import { expect, test } from 'vitest' +import { number, defaulted } from '../../../src' + +test('Value defaulted', () => { + const data = undefined + const res = create(data, defaulted(number(), 42)) + expect(res).toStrictEqual(42) +}) diff --git a/test/validation/defaulted/value.ts b/test/validation/defaulted/value.ts deleted file mode 100644 index dc2acc59..00000000 --- a/test/validation/defaulted/value.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { number, defaulted } from '../../../src' - -export const Struct = defaulted(number(), 42) - -export const data = undefined - -export const output = 42 - -export const create = true diff --git a/test/validation/deprecated/invalid-null.test.ts b/test/validation/deprecated/invalid-null.test.ts new file mode 100644 index 00000000..74c8d4c3 --- /dev/null +++ b/test/validation/deprecated/invalid-null.test.ts @@ -0,0 +1,22 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { deprecated, string } from '../../../src' + +test('Invalid deprecated null', () => { + const data = null + const [err, res] = validate( + data, + deprecated(string(), () => {}) + ) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: null, + type: 'string', + refinement: undefined, + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/deprecated/invalid-null.ts b/test/validation/deprecated/invalid-null.ts deleted file mode 100644 index 03d3524b..00000000 --- a/test/validation/deprecated/invalid-null.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { deprecated, string } from '../../../src' - -export const Struct = deprecated(string(), () => {}) - -export const data = null - -export const failures = [ - { - value: null, - type: 'string', - refinement: undefined, - path: [], - branch: [data], - }, -] diff --git a/test/validation/deprecated/invalid-property.test.ts b/test/validation/deprecated/invalid-property.test.ts new file mode 100644 index 00000000..1e8d47a4 --- /dev/null +++ b/test/validation/deprecated/invalid-property.test.ts @@ -0,0 +1,28 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { deprecated, number, object } from '../../../src' + +test('Invalid deprecated property', () => { + const data = { + deprecatedKey: '42', + } + + const [err, res] = validate( + data, + object({ + deprecatedKey: deprecated(number(), () => {}), + }) + ) + + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: '42', + type: 'number', + refinement: undefined, + path: ['deprecatedKey'], + branch: [data, data.deprecatedKey], + }, + ]) +}) diff --git a/test/validation/deprecated/invalid-property.ts b/test/validation/deprecated/invalid-property.ts deleted file mode 100644 index 8eff69ac..00000000 --- a/test/validation/deprecated/invalid-property.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { deprecated, number, object } from '../../../src' - -export const Struct = object({ - deprecatedKey: deprecated(number(), () => {}), -}) - -export const data = { - deprecatedKey: '42', -} - -export const failures = [ - { - value: '42', - type: 'number', - refinement: undefined, - path: ['deprecatedKey'], - branch: [data, data.deprecatedKey], - }, -] diff --git a/test/validation/deprecated/invalid.test.ts b/test/validation/deprecated/invalid.test.ts new file mode 100644 index 00000000..3b3e697d --- /dev/null +++ b/test/validation/deprecated/invalid.test.ts @@ -0,0 +1,22 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { deprecated, number } from '../../../src' + +test('Invalid deprecated', () => { + const data = '42' + const [err, res] = validate( + data, + deprecated(number(), () => {}) + ) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: '42', + type: 'number', + refinement: undefined, + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/deprecated/invalid.ts b/test/validation/deprecated/invalid.ts deleted file mode 100644 index 54286efb..00000000 --- a/test/validation/deprecated/invalid.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { deprecated, number } from '../../../src' - -export const Struct = deprecated(number(), () => {}) - -export const data = '42' - -export const failures = [ - { - value: '42', - type: 'number', - refinement: undefined, - path: [], - branch: [data], - }, -] diff --git a/test/validation/deprecated/valid-property.test.ts b/test/validation/deprecated/valid-property.test.ts new file mode 100644 index 00000000..200675e3 --- /dev/null +++ b/test/validation/deprecated/valid-property.test.ts @@ -0,0 +1,21 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { type, number, deprecated, any } from '../../../src' + +test('Valid deprecated property', () => { + const data = { + age: 42, + } + + assert( + data, + type({ + name: deprecated(any(), () => {}), + age: number(), + }) + ) + + expect(data).toStrictEqual({ + age: 42, + }) +}) diff --git a/test/validation/deprecated/valid-property.ts b/test/validation/deprecated/valid-property.ts deleted file mode 100644 index ea10861a..00000000 --- a/test/validation/deprecated/valid-property.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { type, number, deprecated, any } from '../../../src' - -export const Struct = type({ - name: deprecated(any(), () => {}), - age: number(), -}) - -export const data = { - age: 42, -} - -export const output = { - age: 42, -} diff --git a/test/validation/deprecated/valid-undefined.test.ts b/test/validation/deprecated/valid-undefined.test.ts new file mode 100644 index 00000000..50e5b929 --- /dev/null +++ b/test/validation/deprecated/valid-undefined.test.ts @@ -0,0 +1,12 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { deprecated, number } from '../../../src' + +test('Valid deprecated undefined', () => { + const data = undefined + assert( + data, + deprecated(number(), () => {}) + ) + expect(data).toStrictEqual(undefined) +}) diff --git a/test/validation/deprecated/valid-undefined.ts b/test/validation/deprecated/valid-undefined.ts deleted file mode 100644 index 38a89b06..00000000 --- a/test/validation/deprecated/valid-undefined.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { deprecated, number } from '../../../src' - -export const Struct = deprecated(number(), () => {}) - -export const data = undefined - -export const output = undefined diff --git a/test/validation/deprecated/valid.test.ts b/test/validation/deprecated/valid.test.ts new file mode 100644 index 00000000..c2ba8ebd --- /dev/null +++ b/test/validation/deprecated/valid.test.ts @@ -0,0 +1,12 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { deprecated, number } from '../../../src' + +test('Valid deprecated', () => { + const data = 42 + assert( + data, + deprecated(number(), () => {}) + ) + expect(data).toStrictEqual(42) +}) diff --git a/test/validation/deprecated/valid.ts b/test/validation/deprecated/valid.ts deleted file mode 100644 index 5bbf414a..00000000 --- a/test/validation/deprecated/valid.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { deprecated, number } from '../../../src' - -export const Struct = deprecated(number(), () => {}) - -export const data = 42 - -export const output = 42 diff --git a/test/validation/dynamic/invalid-reference.test.ts b/test/validation/dynamic/invalid-reference.test.ts new file mode 100644 index 00000000..41f92d87 --- /dev/null +++ b/test/validation/dynamic/invalid-reference.test.ts @@ -0,0 +1,49 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { assert, type, dynamic, literal, number, string } from '../../../src' + +const Entity = type({ + object: string(), +}) + +const User = type({ + object: literal('USER'), + username: string(), +}) + +const Product = type({ + object: literal('PRODUCT'), + price: number(), +}) + +const map = { + USER: User, + PRODUCT: Product, +} + +test('Invalid dynamic reference', () => { + const data = { + object: 'PRODUCT', + price: 'Only $19.99!', + } + + const [err, res] = validate( + data, + dynamic((entity) => { + assert(entity, Entity) + return map[entity.object] + }) + ) + + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 'Only $19.99!', + type: 'number', + refinement: undefined, + path: ['price'], + branch: [data, data.price], + }, + ]) +}) diff --git a/test/validation/dynamic/invalid-reference.ts b/test/validation/dynamic/invalid-reference.ts deleted file mode 100644 index 156b0678..00000000 --- a/test/validation/dynamic/invalid-reference.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { assert, type, dynamic, literal, number, string } from '../../../src' - -const Entity = type({ - object: string(), -}) - -const User = type({ - object: literal('USER'), - username: string(), -}) - -const Product = type({ - object: literal('PRODUCT'), - price: number(), -}) - -const map = { - USER: User, - PRODUCT: Product, -} - -export const Struct = dynamic((entity) => { - assert(entity, Entity) - return map[entity.object] -}) - -export const data = { - object: 'PRODUCT', - price: 'Only $19.99!', -} - -export const failures = [ - { - value: 'Only $19.99!', - type: 'number', - refinement: undefined, - path: ['price'], - branch: [data, data.price], - }, -] diff --git a/test/validation/dynamic/invalid.test.ts b/test/validation/dynamic/invalid.test.ts new file mode 100644 index 00000000..52a502a7 --- /dev/null +++ b/test/validation/dynamic/invalid.test.ts @@ -0,0 +1,22 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { dynamic, string } from '../../../src' + +test('Invalid dynamic', () => { + const data = 3 + const [err, res] = validate( + data, + dynamic(() => string()) + ) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 3, + type: 'string', + refinement: undefined, + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/dynamic/invalid.ts b/test/validation/dynamic/invalid.ts deleted file mode 100644 index b0bb6931..00000000 --- a/test/validation/dynamic/invalid.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { dynamic, string } from '../../../src' - -export const Struct = dynamic(() => string()) - -export const data = 3 - -export const failures = [ - { - value: 3, - type: 'string', - refinement: undefined, - path: [], - branch: [data], - }, -] diff --git a/test/validation/dynamic/valid-reference.test.ts b/test/validation/dynamic/valid-reference.test.ts new file mode 100644 index 00000000..f1d52f48 --- /dev/null +++ b/test/validation/dynamic/valid-reference.test.ts @@ -0,0 +1,41 @@ +import { expect, test } from 'vitest' +import { assert, type, dynamic, literal, string, number } from '../../../src' + +const Entity = type({ + object: string(), +}) + +const User = type({ + object: literal('USER'), + username: string(), +}) + +const Product = type({ + object: literal('PRODUCT'), + price: number(), +}) + +const map = { + USER: User, + PRODUCT: Product, +} + +test('Valid dynamic reference', () => { + const data = { + object: 'PRODUCT', + price: 1999, + } + + assert( + data, + dynamic((entity) => { + assert(entity, Entity) + return map[entity.object] + }) + ) + + expect(data).toStrictEqual({ + object: 'PRODUCT', + price: 1999, + }) +}) diff --git a/test/validation/dynamic/valid-reference.ts b/test/validation/dynamic/valid-reference.ts deleted file mode 100644 index 5156c89d..00000000 --- a/test/validation/dynamic/valid-reference.ts +++ /dev/null @@ -1,35 +0,0 @@ -import { assert, type, dynamic, literal, string, number } from '../../../src' - -const Entity = type({ - object: string(), -}) - -const User = type({ - object: literal('USER'), - username: string(), -}) - -const Product = type({ - object: literal('PRODUCT'), - price: number(), -}) - -const map = { - USER: User, - PRODUCT: Product, -} - -export const Struct = dynamic((entity) => { - assert(entity, Entity) - return map[entity.object] -}) - -export const data = { - object: 'PRODUCT', - price: 1999, -} - -export const output = { - object: 'PRODUCT', - price: 1999, -} diff --git a/test/validation/dynamic/valid.test.ts b/test/validation/dynamic/valid.test.ts new file mode 100644 index 00000000..42d94433 --- /dev/null +++ b/test/validation/dynamic/valid.test.ts @@ -0,0 +1,12 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { dynamic, string } from '../../../src' + +test('Valid dynamic', () => { + const data = 'valid' + assert( + data, + dynamic(() => string()) + ) + expect(data).toStrictEqual('valid') +}) diff --git a/test/validation/dynamic/valid.ts b/test/validation/dynamic/valid.ts deleted file mode 100644 index 8cdb2e21..00000000 --- a/test/validation/dynamic/valid.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { dynamic, string } from '../../../src' - -export const Struct = dynamic(() => string()) - -export const data = 'valid' - -export const output = 'valid' diff --git a/test/validation/dynamic/with-refiners.test.ts b/test/validation/dynamic/with-refiners.test.ts new file mode 100644 index 00000000..d2647629 --- /dev/null +++ b/test/validation/dynamic/with-refiners.test.ts @@ -0,0 +1,22 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { dynamic, string, nonempty } from '../../../src' + +test('With dynamic refiners', () => { + const data = '' + const [err, res] = validate( + data, + dynamic(() => nonempty(string())) + ) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: data, + type: 'string', + refinement: 'nonempty', + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/dynamic/with-refiners.ts b/test/validation/dynamic/with-refiners.ts deleted file mode 100644 index 9da48bce..00000000 --- a/test/validation/dynamic/with-refiners.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { dynamic, string, nonempty } from '../../../src' - -export const Struct = dynamic(() => nonempty(string())) - -export const data = '' - -export const failures = [ - { - value: data, - type: 'string', - refinement: 'nonempty', - path: [], - branch: [data], - }, -] diff --git a/test/validation/empty/invalid-array.test.ts b/test/validation/empty/invalid-array.test.ts new file mode 100644 index 00000000..bf953c78 --- /dev/null +++ b/test/validation/empty/invalid-array.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { array, empty, number } from '../../../src' + +test('Invalid empty array', () => { + const data = [1, 2, 3] + const [err, res] = validate(data, empty(array(number()))) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: data, + type: 'array', + refinement: 'empty', + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/empty/invalid-array.ts b/test/validation/empty/invalid-array.ts deleted file mode 100644 index 27abbfb6..00000000 --- a/test/validation/empty/invalid-array.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { array, empty, number } from '../../../src' - -export const Struct = empty(array(number())) - -export const data = [1, 2, 3] - -export const failures = [ - { - value: data, - type: 'array', - refinement: 'empty', - path: [], - branch: [data], - }, -] diff --git a/test/validation/empty/invalid-map.test.ts b/test/validation/empty/invalid-map.test.ts new file mode 100644 index 00000000..91f83cbc --- /dev/null +++ b/test/validation/empty/invalid-map.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { map, empty, number, string } from '../../../src' + +test('Invalid empty map', () => { + const data = new Map([[1, 'a']]) + const [err, res] = validate(data, empty(map(number(), string()))) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: data, + type: 'map', + refinement: 'empty', + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/empty/invalid-map.ts b/test/validation/empty/invalid-map.ts deleted file mode 100644 index 7deb6535..00000000 --- a/test/validation/empty/invalid-map.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { map, empty, number, string } from '../../../src' - -export const Struct = empty(map(number(), string())) - -export const data = new Map([[1, 'a']]) - -export const failures = [ - { - value: data, - type: 'map', - refinement: 'empty', - path: [], - branch: [data], - }, -] diff --git a/test/validation/empty/invalid-set.test.ts b/test/validation/empty/invalid-set.test.ts new file mode 100644 index 00000000..3b3923cf --- /dev/null +++ b/test/validation/empty/invalid-set.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { set, empty, number } from '../../../src' + +test('Invalid empty set', () => { + const data = new Set([1, 2, 3]) + const [err, res] = validate(data, empty(set(number()))) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: data, + type: 'set', + refinement: 'empty', + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/empty/invalid-set.ts b/test/validation/empty/invalid-set.ts deleted file mode 100644 index 1d21266c..00000000 --- a/test/validation/empty/invalid-set.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { set, empty, number } from '../../../src' - -export const Struct = empty(set(number())) - -export const data = new Set([1, 2, 3]) - -export const failures = [ - { - value: data, - type: 'set', - refinement: 'empty', - path: [], - branch: [data], - }, -] diff --git a/test/validation/empty/invalid-string.test.ts b/test/validation/empty/invalid-string.test.ts new file mode 100644 index 00000000..af797862 --- /dev/null +++ b/test/validation/empty/invalid-string.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { string, empty } from '../../../src' + +test('Invalid empty string', () => { + const data = 'invalid' + const [err, res] = validate(data, empty(string())) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 'invalid', + type: 'string', + refinement: 'empty', + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/empty/invalid-string.ts b/test/validation/empty/invalid-string.ts deleted file mode 100644 index 58e80560..00000000 --- a/test/validation/empty/invalid-string.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { string, empty } from '../../../src' - -export const Struct = empty(string()) - -export const data = 'invalid' - -export const failures = [ - { - value: 'invalid', - type: 'string', - refinement: 'empty', - path: [], - branch: [data], - }, -] diff --git a/test/validation/empty/valid-array.test.ts b/test/validation/empty/valid-array.test.ts new file mode 100644 index 00000000..e5ab8af9 --- /dev/null +++ b/test/validation/empty/valid-array.test.ts @@ -0,0 +1,9 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { number, array, empty } from '../../../src' + +test('Valid empty array', () => { + const data: any[] = [] + assert(data, empty(array(number()))) + expect(data).toStrictEqual([]) +}) diff --git a/test/validation/empty/valid-array.ts b/test/validation/empty/valid-array.ts deleted file mode 100644 index d90e8cbd..00000000 --- a/test/validation/empty/valid-array.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { number, array, empty } from '../../../src' - -export const Struct = empty(array(number())) - -export const data = [] - -export const output = [] diff --git a/test/validation/empty/valid-map.test.ts b/test/validation/empty/valid-map.test.ts new file mode 100644 index 00000000..11cfa405 --- /dev/null +++ b/test/validation/empty/valid-map.test.ts @@ -0,0 +1,9 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { string, number, map, empty } from '../../../src' + +test('Valid empty map', () => { + const data = new Map() + assert(data, empty(map(number(), string()))) + expect(data).toStrictEqual(data) +}) diff --git a/test/validation/empty/valid-map.ts b/test/validation/empty/valid-map.ts deleted file mode 100644 index e203f42e..00000000 --- a/test/validation/empty/valid-map.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { string, number, map, empty } from '../../../src' - -export const Struct = empty(map(number(), string())) - -export const data = new Map() - -export const output = data diff --git a/test/validation/empty/valid-set.test.ts b/test/validation/empty/valid-set.test.ts new file mode 100644 index 00000000..a00a72f7 --- /dev/null +++ b/test/validation/empty/valid-set.test.ts @@ -0,0 +1,9 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { number, set, empty } from '../../../src' + +test('Valid empty set', () => { + const data = new Set() + assert(data, empty(set(number()))) + expect(data).toStrictEqual(data) +}) diff --git a/test/validation/empty/valid-set.ts b/test/validation/empty/valid-set.ts deleted file mode 100644 index a20bb7ca..00000000 --- a/test/validation/empty/valid-set.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { number, set, empty } from '../../../src' - -export const Struct = empty(set(number())) - -export const data = new Set() - -export const output = data diff --git a/test/validation/empty/valid-string.test.ts b/test/validation/empty/valid-string.test.ts new file mode 100644 index 00000000..61d9d98f --- /dev/null +++ b/test/validation/empty/valid-string.test.ts @@ -0,0 +1,9 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { string, empty } from '../../../src' + +test('Valid empty string', () => { + const data = '' + assert(data, empty(string())) + expect(data).toStrictEqual('') +}) diff --git a/test/validation/empty/valid-string.ts b/test/validation/empty/valid-string.ts deleted file mode 100644 index 2925c2e8..00000000 --- a/test/validation/empty/valid-string.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { string, empty } from '../../../src' - -export const Struct = empty(string()) - -export const data = '' - -export const output = '' diff --git a/test/validation/enums/invalid-numbers.test.ts b/test/validation/enums/invalid-numbers.test.ts new file mode 100644 index 00000000..8f6d2162 --- /dev/null +++ b/test/validation/enums/invalid-numbers.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { enums } from '../../../src' + +test('Invalid enums numbers', () => { + const data = 'invalid' + const [err, res] = validate(data, enums([1, 2])) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 'invalid', + type: 'enums', + refinement: undefined, + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/enums/invalid-numbers.ts b/test/validation/enums/invalid-numbers.ts deleted file mode 100644 index 8e3aeb2d..00000000 --- a/test/validation/enums/invalid-numbers.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { enums } from '../../../src' - -export const Struct = enums([1, 2]) - -export const data = 'invalid' - -export const failures = [ - { - value: 'invalid', - type: 'enums', - refinement: undefined, - path: [], - branch: [data], - }, -] diff --git a/test/validation/enums/invalid-strings.test.ts b/test/validation/enums/invalid-strings.test.ts new file mode 100644 index 00000000..5f95c6e7 --- /dev/null +++ b/test/validation/enums/invalid-strings.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { enums } from '../../../src' + +test('Invalid enums strings', () => { + const data = 'invalid' + const [err, res] = validate(data, enums(['one', 'two'])) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 'invalid', + type: 'enums', + refinement: undefined, + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/enums/invalid-strings.ts b/test/validation/enums/invalid-strings.ts deleted file mode 100644 index 2711dcc8..00000000 --- a/test/validation/enums/invalid-strings.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { enums } from '../../../src' - -export const Struct = enums(['one', 'two']) - -export const data = 'invalid' - -export const failures = [ - { - value: 'invalid', - type: 'enums', - refinement: undefined, - path: [], - branch: [data], - }, -] diff --git a/test/validation/enums/valid.test.ts b/test/validation/enums/valid.test.ts new file mode 100644 index 00000000..5093295e --- /dev/null +++ b/test/validation/enums/valid.test.ts @@ -0,0 +1,9 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { enums } from '../../../src' + +test('Valid enums', () => { + const data = 'two' + assert(data, enums(['one', 'two'])) + expect(data).toStrictEqual('two') +}) diff --git a/test/validation/enums/valid.ts b/test/validation/enums/valid.ts deleted file mode 100644 index 5e63da93..00000000 --- a/test/validation/enums/valid.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { enums } from '../../../src' - -export const Struct = enums(['one', 'two']) - -export const data = 'two' - -export const output = 'two' diff --git a/test/validation/function/invalid.test.ts b/test/validation/function/invalid.test.ts new file mode 100644 index 00000000..666f450c --- /dev/null +++ b/test/validation/function/invalid.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { func } from '../../../src' + +test('Invalid function', () => { + const data = false + const [err, res] = validate(data, func()) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: false, + type: 'func', + refinement: undefined, + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/function/invalid.ts b/test/validation/function/invalid.ts deleted file mode 100644 index dbc7bdd8..00000000 --- a/test/validation/function/invalid.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { func } from '../../../src' - -export const Struct = func() - -export const data = false - -export const failures = [ - { - value: false, - type: 'func', - refinement: undefined, - path: [], - branch: [data], - }, -] diff --git a/test/validation/function/valid.test.ts b/test/validation/function/valid.test.ts new file mode 100644 index 00000000..990136bf --- /dev/null +++ b/test/validation/function/valid.test.ts @@ -0,0 +1,9 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { func } from '../../../src' + +test('Valid function', () => { + const data = function () {} + assert(data, func()) + expect(data).toStrictEqual(data) +}) diff --git a/test/validation/function/valid.ts b/test/validation/function/valid.ts deleted file mode 100644 index a9318bbe..00000000 --- a/test/validation/function/valid.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { func } from '../../../src' - -export const Struct = func() - -export const data = function () {} - -export const output = data diff --git a/test/validation/instance/invalid.test.ts b/test/validation/instance/invalid.test.ts new file mode 100644 index 00000000..da6137b1 --- /dev/null +++ b/test/validation/instance/invalid.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { instance } from '../../../src' + +test('Invalid instance', () => { + const data = false + const [err, res] = validate(data, instance(Array)) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: false, + type: 'instance', + refinement: undefined, + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/instance/invalid.ts b/test/validation/instance/invalid.ts deleted file mode 100644 index 73462668..00000000 --- a/test/validation/instance/invalid.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { instance } from '../../../src' - -export const Struct = instance(Array) - -export const data = false - -export const failures = [ - { - value: false, - type: 'instance', - refinement: undefined, - path: [], - branch: [data], - }, -] diff --git a/test/validation/instance/valid.test.ts b/test/validation/instance/valid.test.ts new file mode 100644 index 00000000..d86bf305 --- /dev/null +++ b/test/validation/instance/valid.test.ts @@ -0,0 +1,9 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { instance } from '../../../src' + +test('Valid instance', () => { + const data = [1] + assert(data, instance(Array)) + expect(data).toStrictEqual([1]) +}) diff --git a/test/validation/instance/valid.ts b/test/validation/instance/valid.ts deleted file mode 100644 index 21645966..00000000 --- a/test/validation/instance/valid.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { instance } from '../../../src' - -export const Struct = instance(Array) - -export const data = [1] - -export const output = [1] diff --git a/test/validation/integer/invalid-decimal.test.ts b/test/validation/integer/invalid-decimal.test.ts new file mode 100644 index 00000000..5d7418f7 --- /dev/null +++ b/test/validation/integer/invalid-decimal.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { integer } from '../../../src' + +test('Invalid integer decimal', () => { + const data = 3.14 + const [err, res] = validate(data, integer()) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 3.14, + type: 'integer', + refinement: undefined, + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/integer/invalid-decimal.ts b/test/validation/integer/invalid-decimal.ts deleted file mode 100644 index f39e1185..00000000 --- a/test/validation/integer/invalid-decimal.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { integer } from '../../../src' - -export const Struct = integer() - -export const data = 3.14 - -export const failures = [ - { - value: 3.14, - type: 'integer', - refinement: undefined, - path: [], - branch: [data], - }, -] diff --git a/test/validation/integer/invalid.test.ts b/test/validation/integer/invalid.test.ts new file mode 100644 index 00000000..511cee34 --- /dev/null +++ b/test/validation/integer/invalid.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { integer } from '../../../src' + +test('Invalid integer', () => { + const data = 'invalid' + const [err, res] = validate(data, integer()) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 'invalid', + type: 'integer', + refinement: undefined, + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/integer/invalid.ts b/test/validation/integer/invalid.ts deleted file mode 100644 index d3732b18..00000000 --- a/test/validation/integer/invalid.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { integer } from '../../../src' - -export const Struct = integer() - -export const data = 'invalid' - -export const failures = [ - { - value: 'invalid', - type: 'integer', - refinement: undefined, - path: [], - branch: [data], - }, -] diff --git a/test/validation/integer/valid.test.ts b/test/validation/integer/valid.test.ts new file mode 100644 index 00000000..dce4d4d3 --- /dev/null +++ b/test/validation/integer/valid.test.ts @@ -0,0 +1,9 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { integer } from '../../../src' + +test('Valid integer', () => { + const data = 42 + assert(data, integer()) + expect(data).toStrictEqual(42) +}) diff --git a/test/validation/integer/valid.ts b/test/validation/integer/valid.ts deleted file mode 100644 index 34673bf2..00000000 --- a/test/validation/integer/valid.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { integer } from '../../../src' - -export const Struct = integer() - -export const data = 42 - -export const output = 42 diff --git a/test/validation/intersection/invalid-refinement.test.ts b/test/validation/intersection/invalid-refinement.test.ts new file mode 100644 index 00000000..dc924594 --- /dev/null +++ b/test/validation/intersection/invalid-refinement.test.ts @@ -0,0 +1,22 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { intersection, refine, number } from '../../../src' + +const A = number() +const B = refine(number(), 'positive', (value) => value > 0) + +test('Invalid intersection refinement', () => { + const data = -1 + const [err, res] = validate(data, intersection([A, B])) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + type: 'number', + refinement: 'positive', + value: -1, + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/intersection/invalid-refinement.ts b/test/validation/intersection/invalid-refinement.ts deleted file mode 100644 index 701d7d02..00000000 --- a/test/validation/intersection/invalid-refinement.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { intersection, refine, number } from '../../../src' - -const A = number() -const B = refine(number(), 'positive', (value) => value > 0) - -export const Struct = intersection([A, B]) - -export const data = -1 - -export const failures = [ - { - type: 'number', - refinement: 'positive', - value: -1, - path: [], - branch: [data], - }, -] diff --git a/test/validation/intersection/invalid.test.ts b/test/validation/intersection/invalid.test.ts new file mode 100644 index 00000000..d69cc21c --- /dev/null +++ b/test/validation/intersection/invalid.test.ts @@ -0,0 +1,26 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { type, intersection, string, number } from '../../../src' + +const A = type({ a: string() }) +const B = type({ b: number() }) + +test('Invalid intersection', () => { + const data = { + a: 'a', + b: 'invalid', + } + + const [err, res] = validate(data, intersection([A, B])) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + type: 'number', + value: 'invalid', + refinement: undefined, + path: ['b'], + branch: [data, data.b], + }, + ]) +}) diff --git a/test/validation/intersection/invalid.ts b/test/validation/intersection/invalid.ts deleted file mode 100644 index dab2d018..00000000 --- a/test/validation/intersection/invalid.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { type, intersection, string, number } from '../../../src' - -const A = type({ a: string() }) -const B = type({ b: number() }) - -export const Struct = intersection([A, B]) - -export const data = { - a: 'a', - b: 'invalid', -} - -export const failures = [ - { - type: 'number', - value: 'invalid', - refinement: undefined, - path: ['b'], - branch: [data, data.b], - }, -] diff --git a/test/validation/intersection/valid-refinement.test.ts b/test/validation/intersection/valid-refinement.test.ts new file mode 100644 index 00000000..213e9a71 --- /dev/null +++ b/test/validation/intersection/valid-refinement.test.ts @@ -0,0 +1,12 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { intersection, refine, number } from '../../../src' + +const A = number() +const B = refine(number(), 'positive', (value) => value > 0) + +test('Valid intersection refinement', () => { + const data = 1 + assert(data, intersection([A, B])) + expect(data).toStrictEqual(1) +}) diff --git a/test/validation/intersection/valid-refinement.ts b/test/validation/intersection/valid-refinement.ts deleted file mode 100644 index 27d58c25..00000000 --- a/test/validation/intersection/valid-refinement.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { intersection, refine, number } from '../../../src' - -const A = number() -const B = refine(number(), 'positive', (value) => value > 0) - -export const Struct = intersection([A, B]) - -export const data = 1 - -export const output = 1 diff --git a/test/validation/intersection/valid.test.ts b/test/validation/intersection/valid.test.ts new file mode 100644 index 00000000..992e06e3 --- /dev/null +++ b/test/validation/intersection/valid.test.ts @@ -0,0 +1,20 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { type, intersection, string, number } from '../../../src' + +const A = type({ a: string() }) +const B = type({ b: number() }) + +test('Valid intersection', () => { + const data = { + a: 'a', + b: 42, + } + + assert(data, intersection([A, B])) + + expect(data).toStrictEqual({ + a: 'a', + b: 42, + }) +}) diff --git a/test/validation/intersection/valid.ts b/test/validation/intersection/valid.ts deleted file mode 100644 index 3b2473bf..00000000 --- a/test/validation/intersection/valid.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { type, intersection, string, number } from '../../../src' - -const A = type({ a: string() }) -const B = type({ b: number() }) - -export const Struct = intersection([A, B]) - -export const data = { - a: 'a', - b: 42, -} - -export const output = { - a: 'a', - b: 42, -} diff --git a/test/validation/lazy/invalid.test.ts b/test/validation/lazy/invalid.test.ts new file mode 100644 index 00000000..69a06be7 --- /dev/null +++ b/test/validation/lazy/invalid.test.ts @@ -0,0 +1,22 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { lazy, string } from '../../../src' + +test('Invalid lazy', () => { + const data = 3 + const [err, res] = validate( + data, + lazy(() => string()) + ) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 3, + type: 'string', + refinement: undefined, + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/lazy/invalid.ts b/test/validation/lazy/invalid.ts deleted file mode 100644 index ac79bd1c..00000000 --- a/test/validation/lazy/invalid.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { lazy, string } from '../../../src' - -export const Struct = lazy(() => string()) - -export const data = 3 - -export const failures = [ - { - value: 3, - type: 'string', - refinement: undefined, - path: [], - branch: [data], - }, -] diff --git a/test/validation/lazy/valid.test.ts b/test/validation/lazy/valid.test.ts new file mode 100644 index 00000000..3fa45b18 --- /dev/null +++ b/test/validation/lazy/valid.test.ts @@ -0,0 +1,12 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { lazy, string } from '../../../src' + +test('Valid lazy', () => { + const data = 'two' + assert( + data, + lazy(() => string()) + ) + expect(data).toStrictEqual('two') +}) diff --git a/test/validation/lazy/valid.ts b/test/validation/lazy/valid.ts deleted file mode 100644 index b1802910..00000000 --- a/test/validation/lazy/valid.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { lazy, string } from '../../../src' - -export const Struct = lazy(() => string()) - -export const data = 'two' - -export const output = 'two' diff --git a/test/validation/lazy/with-refiners.test.ts b/test/validation/lazy/with-refiners.test.ts new file mode 100644 index 00000000..813576a9 --- /dev/null +++ b/test/validation/lazy/with-refiners.test.ts @@ -0,0 +1,22 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { lazy, nonempty, string } from '../../../src' + +test('With lazy refiners', () => { + const data = '' + const [err, res] = validate( + data, + lazy(() => nonempty(string())) + ) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: data, + type: 'string', + refinement: 'nonempty', + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/lazy/with-refiners.ts b/test/validation/lazy/with-refiners.ts deleted file mode 100644 index 2261963b..00000000 --- a/test/validation/lazy/with-refiners.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { lazy, nonempty, string } from '../../../src' - -export const Struct = lazy(() => nonempty(string())) - -export const data = '' - -export const failures = [ - { - value: data, - type: 'string', - refinement: 'nonempty', - path: [], - branch: [data], - }, -] diff --git a/test/validation/literal/invalid.test.ts b/test/validation/literal/invalid.test.ts new file mode 100644 index 00000000..0ae52a9c --- /dev/null +++ b/test/validation/literal/invalid.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { literal } from '../../../src' + +test('Invalid literal', () => { + const data = false + const [err, res] = validate(data, literal(42)) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: false, + type: 'literal', + refinement: undefined, + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/literal/invalid.ts b/test/validation/literal/invalid.ts deleted file mode 100644 index f7473a3e..00000000 --- a/test/validation/literal/invalid.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { literal } from '../../../src' - -export const Struct = literal(42) - -export const data = false - -export const failures = [ - { - value: false, - type: 'literal', - refinement: undefined, - path: [], - branch: [data], - }, -] diff --git a/test/validation/literal/valid.test.ts b/test/validation/literal/valid.test.ts new file mode 100644 index 00000000..6b5f912a --- /dev/null +++ b/test/validation/literal/valid.test.ts @@ -0,0 +1,9 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { literal } from '../../../src' + +test('Valid literal', () => { + const data = 42 + assert(data, literal(42)) + expect(data).toStrictEqual(42) +}) diff --git a/test/validation/literal/valid.ts b/test/validation/literal/valid.ts deleted file mode 100644 index 70ea8fb4..00000000 --- a/test/validation/literal/valid.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { literal } from '../../../src' - -export const Struct = literal(42) - -export const data = 42 - -export const output = 42 diff --git a/test/validation/map/invalid-opaque.test.ts b/test/validation/map/invalid-opaque.test.ts new file mode 100644 index 00000000..6e15ac5c --- /dev/null +++ b/test/validation/map/invalid-opaque.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { map } from '../../../src' + +test('Invalid map opaque', () => { + const data = 'invalid' + const [err, res] = validate(data, map()) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 'invalid', + type: 'map', + refinement: undefined, + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/map/invalid-opaque.ts b/test/validation/map/invalid-opaque.ts deleted file mode 100644 index 4dfe413a..00000000 --- a/test/validation/map/invalid-opaque.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { map } from '../../../src' - -export const Struct = map() - -export const data = 'invalid' - -export const failures = [ - { - value: 'invalid', - type: 'map', - refinement: undefined, - path: [], - branch: [data], - }, -] diff --git a/test/validation/map/invalid-property.test.ts b/test/validation/map/invalid-property.test.ts new file mode 100644 index 00000000..dec77f68 --- /dev/null +++ b/test/validation/map/invalid-property.test.ts @@ -0,0 +1,30 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { map, string, number } from '../../../src' + +test('Invalid map property', () => { + const data = new Map([ + ['a', 'a'], + ['b', 'b'], + ]) + + const [err, res] = validate(data, map(string(), number())) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 'a', + type: 'number', + refinement: undefined, + path: ['a'], + branch: [data, 'a'], + }, + { + value: 'b', + type: 'number', + refinement: undefined, + path: ['b'], + branch: [data, 'b'], + }, + ]) +}) diff --git a/test/validation/map/invalid-property.ts b/test/validation/map/invalid-property.ts deleted file mode 100644 index a89c3a3f..00000000 --- a/test/validation/map/invalid-property.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { map, string, number } from '../../../src' - -export const Struct = map(string(), number()) - -export const data = new Map([ - ['a', 'a'], - ['b', 'b'], -]) - -export const failures = [ - { - value: 'a', - type: 'number', - refinement: undefined, - path: ['a'], - branch: [data, 'a'], - }, - { - value: 'b', - type: 'number', - refinement: undefined, - path: ['b'], - branch: [data, 'b'], - }, -] diff --git a/test/validation/map/invalid.test.ts b/test/validation/map/invalid.test.ts new file mode 100644 index 00000000..562c1665 --- /dev/null +++ b/test/validation/map/invalid.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { map, string, number } from '../../../src' + +test('Invalid map', () => { + const data = 'invalid' + const [err, res] = validate(data, map(string(), number())) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 'invalid', + type: 'map', + refinement: undefined, + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/map/invalid.ts b/test/validation/map/invalid.ts deleted file mode 100644 index 9de01047..00000000 --- a/test/validation/map/invalid.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { map, string, number } from '../../../src' - -export const Struct = map(string(), number()) - -export const data = 'invalid' - -export const failures = [ - { - value: 'invalid', - type: 'map', - refinement: undefined, - path: [], - branch: [data], - }, -] diff --git a/test/validation/map/valid-opaque.test.ts b/test/validation/map/valid-opaque.test.ts new file mode 100644 index 00000000..8b3edd0c --- /dev/null +++ b/test/validation/map/valid-opaque.test.ts @@ -0,0 +1,19 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { map } from '../../../src' + +test('Valid map opaque', () => { + const data = new Map([ + ['a', 1], + [2, true], + ] as any) + + assert(data, map()) + + expect(data).toStrictEqual( + new Map([ + ['a', 1], + [2, true], + ] as any) + ) +}) diff --git a/test/validation/map/valid-opaque.ts b/test/validation/map/valid-opaque.ts deleted file mode 100644 index 63d0f38c..00000000 --- a/test/validation/map/valid-opaque.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { map } from '../../../src' - -export const Struct = map() - -export const data = new Map([ - ['a', 1], - [2, true], -] as any) - -export const output = data diff --git a/test/validation/map/valid.test.ts b/test/validation/map/valid.test.ts new file mode 100644 index 00000000..9224345e --- /dev/null +++ b/test/validation/map/valid.test.ts @@ -0,0 +1,19 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { map, string, number } from '../../../src' + +test('Valid map', () => { + const data = new Map([ + ['a', 1], + ['b', 2], + ]) + + assert(data, map(string(), number())) + + expect(data).toStrictEqual( + new Map([ + ['a', 1], + ['b', 2], + ]) + ) +}) diff --git a/test/validation/map/valid.ts b/test/validation/map/valid.ts deleted file mode 100644 index e7a24d7e..00000000 --- a/test/validation/map/valid.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { map, string, number } from '../../../src' - -export const Struct = map(string(), number()) - -export const data = new Map([ - ['a', 1], - ['b', 2], -]) - -export const output = new Map([ - ['a', 1], - ['b', 2], -]) diff --git a/test/validation/max/invalid-exclusive.test.ts b/test/validation/max/invalid-exclusive.test.ts new file mode 100644 index 00000000..fc835105 --- /dev/null +++ b/test/validation/max/invalid-exclusive.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { number, max } from '../../../src' + +test('Invalid max exclusive', () => { + const data = 0 + const [err, res] = validate(data, max(number(), 0, { exclusive: true })) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 0, + type: 'number', + refinement: 'max', + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/max/invalid-exclusive.ts b/test/validation/max/invalid-exclusive.ts deleted file mode 100644 index 3e4f43f6..00000000 --- a/test/validation/max/invalid-exclusive.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { number, max } from '../../../src' - -export const Struct = max(number(), 0, { exclusive: true }) - -export const data = 0 - -export const failures = [ - { - value: 0, - type: 'number', - refinement: 'max', - path: [], - branch: [data], - }, -] diff --git a/test/validation/max/invalid.test.ts b/test/validation/max/invalid.test.ts new file mode 100644 index 00000000..d6e8b693 --- /dev/null +++ b/test/validation/max/invalid.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { number, max } from '../../../src' + +test('Invalid max', () => { + const data = 1 + const [err, res] = validate(data, max(number(), 0)) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 1, + type: 'number', + refinement: 'max', + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/max/invalid.ts b/test/validation/max/invalid.ts deleted file mode 100644 index 324ecd21..00000000 --- a/test/validation/max/invalid.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { number, max } from '../../../src' - -export const Struct = max(number(), 0) - -export const data = 1 - -export const failures = [ - { - value: 1, - type: 'number', - refinement: 'max', - path: [], - branch: [data], - }, -] diff --git a/test/validation/max/valid-inclusive.test.ts b/test/validation/max/valid-inclusive.test.ts new file mode 100644 index 00000000..e9673cbf --- /dev/null +++ b/test/validation/max/valid-inclusive.test.ts @@ -0,0 +1,9 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { number, max } from '../../../src' + +test('Valid max inclusive', () => { + const data = 0 + assert(data, max(number(), 0)) + expect(data).toStrictEqual(0) +}) diff --git a/test/validation/max/valid-inclusive.ts b/test/validation/max/valid-inclusive.ts deleted file mode 100644 index b76c753d..00000000 --- a/test/validation/max/valid-inclusive.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { number, max } from '../../../src' - -export const Struct = max(number(), 0) - -export const data = 0 - -export const output = 0 diff --git a/test/validation/max/valid.test.ts b/test/validation/max/valid.test.ts new file mode 100644 index 00000000..0fc87e32 --- /dev/null +++ b/test/validation/max/valid.test.ts @@ -0,0 +1,9 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { number, max } from '../../../src' + +test('Valid max', () => { + const data = -1 + assert(data, max(number(), 0)) + expect(data).toStrictEqual(-1) +}) diff --git a/test/validation/max/valid.ts b/test/validation/max/valid.ts deleted file mode 100644 index 5a94da8c..00000000 --- a/test/validation/max/valid.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { number, max } from '../../../src' - -export const Struct = max(number(), 0) - -export const data = -1 - -export const output = -1 diff --git a/test/validation/min/invalid-exclusive.test.ts b/test/validation/min/invalid-exclusive.test.ts new file mode 100644 index 00000000..32fc5407 --- /dev/null +++ b/test/validation/min/invalid-exclusive.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { number, min } from '../../../src' + +test('Invalid min exclusive', () => { + const data = 0 + const [err, res] = validate(data, min(number(), 0, { exclusive: true })) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 0, + type: 'number', + refinement: 'min', + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/min/invalid-exclusive.ts b/test/validation/min/invalid-exclusive.ts deleted file mode 100644 index 5ece76ef..00000000 --- a/test/validation/min/invalid-exclusive.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { number, min } from '../../../src' - -export const Struct = min(number(), 0, { exclusive: true }) - -export const data = 0 - -export const failures = [ - { - value: 0, - type: 'number', - refinement: 'min', - path: [], - branch: [data], - }, -] diff --git a/test/validation/min/invalid.test.ts b/test/validation/min/invalid.test.ts new file mode 100644 index 00000000..74aa3404 --- /dev/null +++ b/test/validation/min/invalid.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { number, min } from '../../../src' + +test('Invalid min', () => { + const data = -1 + const [err, res] = validate(data, min(number(), 0)) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: -1, + type: 'number', + refinement: 'min', + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/min/invalid.ts b/test/validation/min/invalid.ts deleted file mode 100644 index 72f4e1ad..00000000 --- a/test/validation/min/invalid.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { number, min } from '../../../src' - -export const Struct = min(number(), 0) - -export const data = -1 - -export const failures = [ - { - value: -1, - type: 'number', - refinement: 'min', - path: [], - branch: [data], - }, -] diff --git a/test/validation/min/valid-inclusive.test.ts b/test/validation/min/valid-inclusive.test.ts new file mode 100644 index 00000000..7fe72045 --- /dev/null +++ b/test/validation/min/valid-inclusive.test.ts @@ -0,0 +1,9 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { number, min } from '../../../src' + +test('Valid min inclusive', () => { + const data = 0 + assert(data, min(number(), 0)) + expect(data).toStrictEqual(0) +}) diff --git a/test/validation/min/valid-inclusive.ts b/test/validation/min/valid-inclusive.ts deleted file mode 100644 index 48b0c9ea..00000000 --- a/test/validation/min/valid-inclusive.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { number, min } from '../../../src' - -export const Struct = min(number(), 0) - -export const data = 0 - -export const output = 0 diff --git a/test/validation/min/valid.test.ts b/test/validation/min/valid.test.ts new file mode 100644 index 00000000..bbcfbee2 --- /dev/null +++ b/test/validation/min/valid.test.ts @@ -0,0 +1,9 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { number, min } from '../../../src' + +test('Valid min', () => { + const data = 3 + assert(data, min(number(), 0)) + expect(data).toStrictEqual(3) +}) diff --git a/test/validation/min/valid.ts b/test/validation/min/valid.ts deleted file mode 100644 index c33d7f6b..00000000 --- a/test/validation/min/valid.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { number, min } from '../../../src' - -export const Struct = min(number(), 0) - -export const data = 3 - -export const output = 3 diff --git a/test/validation/never/invalid.test.ts b/test/validation/never/invalid.test.ts new file mode 100644 index 00000000..8965a6e9 --- /dev/null +++ b/test/validation/never/invalid.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { never } from '../../../src' + +test('Invalid never', () => { + const data = true + const [err, res] = validate(data, never()) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: true, + type: 'never', + refinement: undefined, + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/never/invalid.ts b/test/validation/never/invalid.ts deleted file mode 100644 index 8b99fb39..00000000 --- a/test/validation/never/invalid.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { never } from '../../../src' - -export const Struct = never() - -export const data = true - -export const failures = [ - { - value: true, - type: 'never', - refinement: undefined, - path: [], - branch: [data], - }, -] diff --git a/test/validation/nullable/invalid.test.ts b/test/validation/nullable/invalid.test.ts new file mode 100644 index 00000000..8df7b8e9 --- /dev/null +++ b/test/validation/nullable/invalid.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { number, nullable } from '../../../src' + +test('Invalid nullable', () => { + const data = 'invalid' + const [err, res] = validate(data, nullable(number())) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 'invalid', + type: 'number', + refinement: undefined, + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/nullable/invalid.ts b/test/validation/nullable/invalid.ts deleted file mode 100644 index 028d879e..00000000 --- a/test/validation/nullable/invalid.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { number, nullable } from '../../../src' - -export const Struct = nullable(number()) - -export const data = 'invalid' - -export const failures = [ - { - value: 'invalid', - type: 'number', - refinement: undefined, - path: [], - branch: [data], - }, -] diff --git a/test/validation/nullable/valid-defined-nested.test.ts b/test/validation/nullable/valid-defined-nested.test.ts new file mode 100644 index 00000000..75e76f37 --- /dev/null +++ b/test/validation/nullable/valid-defined-nested.test.ts @@ -0,0 +1,23 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { type, string, number, nullable } from '../../../src' + +test('Valid nullable defined nested', () => { + const data = { + name: 'Jill', + age: 42, + } + + assert( + data, + type({ + name: nullable(string()), + age: number(), + }) + ) + + expect(data).toStrictEqual({ + name: 'Jill', + age: 42, + }) +}) diff --git a/test/validation/nullable/valid-defined-nested.ts b/test/validation/nullable/valid-defined-nested.ts deleted file mode 100644 index b357ba8e..00000000 --- a/test/validation/nullable/valid-defined-nested.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { type, string, number, nullable } from '../../../src' - -export const Struct = type({ - name: nullable(string()), - age: number(), -}) - -export const data = { - name: 'Jill', - age: 42, -} - -export const output = { - name: 'Jill', - age: 42, -} diff --git a/test/validation/nullable/valid-defined.test.ts b/test/validation/nullable/valid-defined.test.ts new file mode 100644 index 00000000..1820a30b --- /dev/null +++ b/test/validation/nullable/valid-defined.test.ts @@ -0,0 +1,9 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { number, nullable } from '../../../src' + +test('Valid nullable defined', () => { + const data = 42 + assert(data, nullable(number())) + expect(data).toStrictEqual(42) +}) diff --git a/test/validation/nullable/valid-defined.ts b/test/validation/nullable/valid-defined.ts deleted file mode 100644 index 18eb8a74..00000000 --- a/test/validation/nullable/valid-defined.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { number, nullable } from '../../../src' - -export const Struct = nullable(number()) - -export const data = 42 - -export const output = 42 diff --git a/test/validation/nullable/valid-null-nested.test.ts b/test/validation/nullable/valid-null-nested.test.ts new file mode 100644 index 00000000..488c431b --- /dev/null +++ b/test/validation/nullable/valid-null-nested.test.ts @@ -0,0 +1,23 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { type, string, number, nullable } from '../../../src' + +test('Valid nullable null nested', () => { + const data = { + name: null, + age: 42, + } + + assert( + data, + type({ + name: nullable(string()), + age: number(), + }) + ) + + expect(data).toStrictEqual({ + name: null, + age: 42, + }) +}) diff --git a/test/validation/nullable/valid-null-nested.ts b/test/validation/nullable/valid-null-nested.ts deleted file mode 100644 index 9db75acb..00000000 --- a/test/validation/nullable/valid-null-nested.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { type, string, number, nullable } from '../../../src' - -export const Struct = type({ - name: nullable(string()), - age: number(), -}) - -export const data = { - name: null, - age: 42, -} - -export const output = { - name: null, - age: 42, -} diff --git a/test/validation/nullable/valid-null.test.ts b/test/validation/nullable/valid-null.test.ts new file mode 100644 index 00000000..0e91f6ba --- /dev/null +++ b/test/validation/nullable/valid-null.test.ts @@ -0,0 +1,9 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { number, nullable } from '../../../src' + +test('Valid nullable null', () => { + const data = null + assert(data, nullable(number())) + expect(data).toStrictEqual(null) +}) diff --git a/test/validation/nullable/valid-null.ts b/test/validation/nullable/valid-null.ts deleted file mode 100644 index 528493f0..00000000 --- a/test/validation/nullable/valid-null.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { number, nullable } from '../../../src' - -export const Struct = nullable(number()) - -export const data = null - -export const output = null diff --git a/test/validation/number/invalid.test.ts b/test/validation/number/invalid.test.ts new file mode 100644 index 00000000..da946a34 --- /dev/null +++ b/test/validation/number/invalid.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { number } from '../../../src' + +test('Invalid number', () => { + const data = 'invalid' + const [err, res] = validate(data, number()) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 'invalid', + type: 'number', + refinement: undefined, + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/number/invalid.ts b/test/validation/number/invalid.ts deleted file mode 100644 index 140420e7..00000000 --- a/test/validation/number/invalid.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { number } from '../../../src' - -export const Struct = number() - -export const data = 'invalid' - -export const failures = [ - { - value: 'invalid', - type: 'number', - refinement: undefined, - path: [], - branch: [data], - }, -] diff --git a/test/validation/number/valid.test.ts b/test/validation/number/valid.test.ts new file mode 100644 index 00000000..6f54c5c9 --- /dev/null +++ b/test/validation/number/valid.test.ts @@ -0,0 +1,9 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { number } from '../../../src' + +test('Valid number', () => { + const data = 42 + assert(data, number()) + expect(data).toStrictEqual(42) +}) diff --git a/test/validation/number/valid.ts b/test/validation/number/valid.ts deleted file mode 100644 index 8ea8b9a2..00000000 --- a/test/validation/number/valid.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { number } from '../../../src' - -export const Struct = number() - -export const data = 42 - -export const output = 42 diff --git a/test/validation/object/invalid-array.test.ts b/test/validation/object/invalid-array.test.ts new file mode 100644 index 00000000..1fa004e8 --- /dev/null +++ b/test/validation/object/invalid-array.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { object } from '../../../src' + +test('Invalid object array', () => { + const data: any[] = [] + const [err, res] = validate(data, object()) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: [], + type: 'object', + refinement: undefined, + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/object/invalid-array.ts b/test/validation/object/invalid-array.ts deleted file mode 100644 index 0555dae0..00000000 --- a/test/validation/object/invalid-array.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { object } from '../../../src' - -export const Struct = object() - -export const data = [] - -export const failures = [ - { - value: [], - type: 'object', - refinement: undefined, - path: [], - branch: [data], - }, -] diff --git a/test/validation/object/invalid-element-nested.test.ts b/test/validation/object/invalid-element-nested.test.ts new file mode 100644 index 00000000..0048e494 --- /dev/null +++ b/test/validation/object/invalid-element-nested.test.ts @@ -0,0 +1,30 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { object, array, string } from '../../../src' + +test('Invalid object element nested', () => { + const data = { + name: 'john', + emails: ['name@example.com', false], + } + + const [err, res] = validate( + data, + object({ + name: string(), + emails: array(string()), + }) + ) + + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: false, + type: 'string', + refinement: undefined, + path: ['emails', 1], + branch: [data, data.emails, data.emails[1]], + }, + ]) +}) diff --git a/test/validation/object/invalid-element-nested.ts b/test/validation/object/invalid-element-nested.ts deleted file mode 100644 index e442244b..00000000 --- a/test/validation/object/invalid-element-nested.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { object, array, string } from '../../../src' - -export const Struct = object({ - name: string(), - emails: array(string()), -}) - -export const data = { - name: 'john', - emails: ['name@example.com', false], -} - -export const failures = [ - { - value: false, - type: 'string', - refinement: undefined, - path: ['emails', 1], - branch: [data, data.emails, data.emails[1]], - }, -] diff --git a/test/validation/object/invalid-opaque.test.ts b/test/validation/object/invalid-opaque.test.ts new file mode 100644 index 00000000..14f8059d --- /dev/null +++ b/test/validation/object/invalid-opaque.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { object } from '../../../src' + +test('Invalid object opaque', () => { + const data = 'invalid' + const [err, res] = validate(data, object()) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 'invalid', + type: 'object', + refinement: undefined, + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/object/invalid-opaque.ts b/test/validation/object/invalid-opaque.ts deleted file mode 100644 index c2f1918c..00000000 --- a/test/validation/object/invalid-opaque.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { object } from '../../../src' - -export const Struct = object() - -export const data = 'invalid' - -export const failures = [ - { - value: 'invalid', - type: 'object', - refinement: undefined, - path: [], - branch: [data], - }, -] diff --git a/test/validation/object/invalid-property-nested.test.ts b/test/validation/object/invalid-property-nested.test.ts new file mode 100644 index 00000000..19499812 --- /dev/null +++ b/test/validation/object/invalid-property-nested.test.ts @@ -0,0 +1,36 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { object, string } from '../../../src' + +test('Invalid object property nested', () => { + const data = { + name: 'john', + address: { + street: 123, + city: 'Springfield', + }, + } + + const [err, res] = validate( + data, + object({ + name: string(), + address: object({ + street: string(), + city: string(), + }), + }) + ) + + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 123, + type: 'string', + refinement: undefined, + path: ['address', 'street'], + branch: [data, data.address, data.address.street], + }, + ]) +}) diff --git a/test/validation/object/invalid-property-nested.ts b/test/validation/object/invalid-property-nested.ts deleted file mode 100644 index 93960c9b..00000000 --- a/test/validation/object/invalid-property-nested.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { object, string } from '../../../src' - -export const Struct = object({ - name: string(), - address: object({ - street: string(), - city: string(), - }), -}) - -export const data = { - name: 'john', - address: { - street: 123, - city: 'Springfield', - }, -} - -export const failures = [ - { - value: 123, - type: 'string', - refinement: undefined, - path: ['address', 'street'], - branch: [data, data.address, data.address.street], - }, -] diff --git a/test/validation/object/invalid-property-unknown.test.ts b/test/validation/object/invalid-property-unknown.test.ts new file mode 100644 index 00000000..0af71381 --- /dev/null +++ b/test/validation/object/invalid-property-unknown.test.ts @@ -0,0 +1,31 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { object, string, number } from '../../../src' + +test('Invalid object property unknown', () => { + const data = { + name: 'john', + age: 42, + unknown: true, + } + + const [err, res] = validate( + data, + object({ + name: string(), + age: number(), + }) + ) + + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: true, + type: 'never', + refinement: undefined, + path: ['unknown'], + branch: [data, data.unknown], + }, + ]) +}) diff --git a/test/validation/object/invalid-property-unknown.ts b/test/validation/object/invalid-property-unknown.ts deleted file mode 100644 index b52d2479..00000000 --- a/test/validation/object/invalid-property-unknown.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { object, string, number } from '../../../src' - -export const Struct = object({ - name: string(), - age: number(), -}) - -export const data = { - name: 'john', - age: 42, - unknown: true, -} - -export const failures = [ - { - value: true, - type: 'never', - refinement: undefined, - path: ['unknown'], - branch: [data, data.unknown], - }, -] diff --git a/test/validation/object/invalid-property.test.ts b/test/validation/object/invalid-property.test.ts new file mode 100644 index 00000000..7cc205eb --- /dev/null +++ b/test/validation/object/invalid-property.test.ts @@ -0,0 +1,39 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { object, string, number } from '../../../src' + +test('Invalid object property', () => { + const data = { + name: 'john', + age: 'invalid', + height: 2, + } + + const [err, res] = validate( + data, + object({ + name: string(), + age: number(), + height: string(), + }) + ) + + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 'invalid', + type: 'number', + refinement: undefined, + path: ['age'], + branch: [data, data.age], + }, + { + value: 2, + type: 'string', + refinement: undefined, + path: ['height'], + branch: [data, data.height], + }, + ]) +}) diff --git a/test/validation/object/invalid-property.ts b/test/validation/object/invalid-property.ts deleted file mode 100644 index 1c575b99..00000000 --- a/test/validation/object/invalid-property.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { object, string, number } from '../../../src' - -export const Struct = object({ - name: string(), - age: number(), - height: string(), -}) - -export const data = { - name: 'john', - age: 'invalid', - height: 2, -} - -export const failures = [ - { - value: 'invalid', - type: 'number', - refinement: undefined, - path: ['age'], - branch: [data, data.age], - }, - { - value: 2, - type: 'string', - refinement: undefined, - path: ['height'], - branch: [data, data.height], - }, -] diff --git a/test/validation/object/invalid-referential.test.ts b/test/validation/object/invalid-referential.test.ts new file mode 100644 index 00000000..b4549c3a --- /dev/null +++ b/test/validation/object/invalid-referential.test.ts @@ -0,0 +1,36 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { object, string, pattern, refine } from '../../../src' + +const Section = pattern(string(), /^\d+(\.\d+)*$/) + +test('Invalid object referential', () => { + const data = { + section: '1', + subsection: '2.1', + } + + const [err, res] = validate( + data, + object({ + section: Section, + subsection: refine(Section, 'Subsection', (value, ctx) => { + const { branch } = ctx + const parent = branch[0] + return value.startsWith(`${parent.section}.`) + }), + }) + ) + + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: '2.1', + type: 'string', + refinement: 'Subsection', + path: ['subsection'], + branch: [data, data.subsection], + }, + ]) +}) diff --git a/test/validation/object/invalid-referential.ts b/test/validation/object/invalid-referential.ts deleted file mode 100644 index de3b745f..00000000 --- a/test/validation/object/invalid-referential.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { object, string, pattern, refine } from '../../../src' - -const Section = pattern(string(), /^\d+(\.\d+)*$/) - -export const Struct = object({ - section: Section, - subsection: refine(Section, 'Subsection', (value, ctx) => { - const { branch } = ctx - const parent = branch[0] - return value.startsWith(`${parent.section}.`) - }), -}) - -export const data = { - section: '1', - subsection: '2.1', -} - -export const failures = [ - { - value: '2.1', - type: 'string', - refinement: 'Subsection', - path: ['subsection'], - branch: [data, data.subsection], - }, -] diff --git a/test/validation/object/invalid.test.ts b/test/validation/object/invalid.test.ts new file mode 100644 index 00000000..91dc3ce3 --- /dev/null +++ b/test/validation/object/invalid.test.ts @@ -0,0 +1,27 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { object, string, number } from '../../../src' + +test('Invalid object', () => { + const data = 'invalid' + + const [err, res] = validate( + data, + object({ + name: string(), + age: number(), + }) + ) + + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 'invalid', + type: 'object', + refinement: undefined, + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/object/invalid.ts b/test/validation/object/invalid.ts deleted file mode 100644 index 05252e82..00000000 --- a/test/validation/object/invalid.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { object, string, number } from '../../../src' - -export const Struct = object({ - name: string(), - age: number(), -}) - -export const data = 'invalid' - -export const failures = [ - { - value: 'invalid', - type: 'object', - refinement: undefined, - path: [], - branch: [data], - }, -] diff --git a/test/validation/object/valid-frozen.test.ts b/test/validation/object/valid-frozen.test.ts new file mode 100644 index 00000000..f196c125 --- /dev/null +++ b/test/validation/object/valid-frozen.test.ts @@ -0,0 +1,23 @@ +import { create } from '../../../src' +import { expect, test } from 'vitest' +import { object, string, number } from '../../../src' + +test('Valid object frozen', () => { + const data = Object.freeze({ + name: 'john', + age: 42, + }) + + const res = create( + data, + object({ + name: string(), + age: number(), + }) + ) + + expect(res).toStrictEqual({ + name: 'john', + age: 42, + }) +}) diff --git a/test/validation/object/valid-frozen.ts b/test/validation/object/valid-frozen.ts deleted file mode 100644 index 462de9f1..00000000 --- a/test/validation/object/valid-frozen.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { object, string, number } from '../../../src' - -export const Struct = object({ - name: string(), - age: number(), -}) - -export const data = Object.freeze({ - name: 'john', - age: 42, -}) - -export const output = { - name: 'john', - age: 42, -} - -export const create = true diff --git a/test/validation/object/valid-nested.test.ts b/test/validation/object/valid-nested.test.ts new file mode 100644 index 00000000..bddcafb5 --- /dev/null +++ b/test/validation/object/valid-nested.test.ts @@ -0,0 +1,32 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { object, string } from '../../../src' + +test('Valid object nested', () => { + const data = { + name: 'john', + address: { + street: '123 Fake St', + city: 'Springfield', + }, + } + + assert( + data, + object({ + name: string(), + address: object({ + street: string(), + city: string(), + }), + }) + ) + + expect(data).toStrictEqual({ + name: 'john', + address: { + street: '123 Fake St', + city: 'Springfield', + }, + }) +}) diff --git a/test/validation/object/valid-nested.ts b/test/validation/object/valid-nested.ts deleted file mode 100644 index e7c46940..00000000 --- a/test/validation/object/valid-nested.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { object, string } from '../../../src' - -export const Struct = object({ - name: string(), - address: object({ - street: string(), - city: string(), - }), -}) - -export const data = { - name: 'john', - address: { - street: '123 Fake St', - city: 'Springfield', - }, -} - -export const output = { - name: 'john', - address: { - street: '123 Fake St', - city: 'Springfield', - }, -} diff --git a/test/validation/object/valid-opaque.test.ts b/test/validation/object/valid-opaque.test.ts new file mode 100644 index 00000000..c03c8aad --- /dev/null +++ b/test/validation/object/valid-opaque.test.ts @@ -0,0 +1,17 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { object } from '../../../src' + +test('Valid object opaque', () => { + const data = { + a: 'string', + b: 42, + } + + assert(data, object()) + + expect(data).toStrictEqual({ + a: 'string', + b: 42, + }) +}) diff --git a/test/validation/object/valid-opaque.ts b/test/validation/object/valid-opaque.ts deleted file mode 100644 index 9240a5bf..00000000 --- a/test/validation/object/valid-opaque.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { object } from '../../../src' - -export const Struct = object() - -export const data = { - a: 'string', - b: 42, -} - -export const output = { - a: 'string', - b: 42, -} diff --git a/test/validation/object/valid-referential.test.ts b/test/validation/object/valid-referential.test.ts new file mode 100644 index 00000000..e938dc40 --- /dev/null +++ b/test/validation/object/valid-referential.test.ts @@ -0,0 +1,29 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { object, string, pattern, refine } from '../../../src' + +const Section = pattern(string(), /^\d+(\.\d+)*$/) + +test('Valid object referential', () => { + const data = { + section: '1', + subsection: '1.1', + } + + assert( + data, + object({ + section: Section, + subsection: refine(Section, 'Subsection', (value, ctx) => { + const { branch } = ctx + const parent = branch[0] + return value.startsWith(`${parent.section}.`) + }), + }) + ) + + expect(data).toStrictEqual({ + section: '1', + subsection: '1.1', + }) +}) diff --git a/test/validation/object/valid-referential.ts b/test/validation/object/valid-referential.ts deleted file mode 100644 index 304dd75d..00000000 --- a/test/validation/object/valid-referential.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { object, string, pattern, refine } from '../../../src' - -const Section = pattern(string(), /^\d+(\.\d+)*$/) - -export const Struct = object({ - section: Section, - subsection: refine(Section, 'Subsection', (value, ctx) => { - const { branch } = ctx - const parent = branch[0] - return value.startsWith(`${parent.section}.`) - }), -}) - -export const data = { - section: '1', - subsection: '1.1', -} - -export const output = { - section: '1', - subsection: '1.1', -} diff --git a/test/validation/object/valid.test.ts b/test/validation/object/valid.test.ts new file mode 100644 index 00000000..d195a92d --- /dev/null +++ b/test/validation/object/valid.test.ts @@ -0,0 +1,23 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { object, string, number } from '../../../src' + +test('Valid object', () => { + const data = { + name: 'john', + age: 42, + } + + assert( + data, + object({ + name: string(), + age: number(), + }) + ) + + expect(data).toStrictEqual({ + name: 'john', + age: 42, + }) +}) diff --git a/test/validation/object/valid.ts b/test/validation/object/valid.ts deleted file mode 100644 index 25833387..00000000 --- a/test/validation/object/valid.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { object, string, number } from '../../../src' - -export const Struct = object({ - name: string(), - age: number(), -}) - -export const data = { - name: 'john', - age: 42, -} - -export const output = { - name: 'john', - age: 42, -} diff --git a/test/validation/omit/invalid-element-nested.test.ts b/test/validation/omit/invalid-element-nested.test.ts new file mode 100644 index 00000000..aaf6fc0f --- /dev/null +++ b/test/validation/omit/invalid-element-nested.test.ts @@ -0,0 +1,32 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { omit, object, array, string } from '../../../src' + +test('Invalid omit element nested', () => { + const data = { + emails: ['name@example.com', false], + } + + const [err, res] = validate( + data, + omit( + object({ + name: string(), + emails: array(string()), + }), + ['name'] + ) + ) + + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: false, + type: 'string', + refinement: undefined, + path: ['emails', 1], + branch: [data, data.emails, data.emails[1]], + }, + ]) +}) diff --git a/test/validation/omit/invalid-element-nested.ts b/test/validation/omit/invalid-element-nested.ts deleted file mode 100644 index 55150b66..00000000 --- a/test/validation/omit/invalid-element-nested.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { omit, object, array, string } from '../../../src' - -export const Struct = omit( - object({ - name: string(), - emails: array(string()), - }), - ['name'] -) - -export const data = { - emails: ['name@example.com', false], -} - -export const failures = [ - { - value: false, - type: 'string', - refinement: undefined, - path: ['emails', 1], - branch: [data, data.emails, data.emails[1]], - }, -] diff --git a/test/validation/omit/invalid-property-nested.test.ts b/test/validation/omit/invalid-property-nested.test.ts new file mode 100644 index 00000000..107584e9 --- /dev/null +++ b/test/validation/omit/invalid-property-nested.test.ts @@ -0,0 +1,38 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { omit, object, string } from '../../../src' + +test('Invalid omit property nested', () => { + const data = { + address: { + street: 123, + city: 'Springfield', + }, + } + + const [err, res] = validate( + data, + omit( + object({ + name: string(), + address: object({ + street: string(), + city: string(), + }), + }), + ['name'] + ) + ) + + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 123, + type: 'string', + refinement: undefined, + path: ['address', 'street'], + branch: [data, data.address, data.address.street], + }, + ]) +}) diff --git a/test/validation/omit/invalid-property-nested.ts b/test/validation/omit/invalid-property-nested.ts deleted file mode 100644 index f4a45115..00000000 --- a/test/validation/omit/invalid-property-nested.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { omit, object, string } from '../../../src' - -export const Struct = omit( - object({ - name: string(), - address: object({ - street: string(), - city: string(), - }), - }), - ['name'] -) - -export const data = { - address: { - street: 123, - city: 'Springfield', - }, -} - -export const failures = [ - { - value: 123, - type: 'string', - refinement: undefined, - path: ['address', 'street'], - branch: [data, data.address, data.address.street], - }, -] diff --git a/test/validation/omit/invalid-property-unknown.test.ts b/test/validation/omit/invalid-property-unknown.test.ts new file mode 100644 index 00000000..9e48d27e --- /dev/null +++ b/test/validation/omit/invalid-property-unknown.test.ts @@ -0,0 +1,33 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { omit, object, string, number } from '../../../src' + +test('Invalid omit property unknown', () => { + const data = { + name: 'john', + age: 42, + } + + const [err, res] = validate( + data, + omit( + object({ + name: string(), + age: number(), + }), + ['age'] + ) + ) + + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 42, + type: 'never', + refinement: undefined, + path: ['age'], + branch: [data, data.age], + }, + ]) +}) diff --git a/test/validation/omit/invalid-property-unknown.ts b/test/validation/omit/invalid-property-unknown.ts deleted file mode 100644 index 055c18e6..00000000 --- a/test/validation/omit/invalid-property-unknown.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { omit, object, string, number } from '../../../src' - -export const Struct = omit( - object({ - name: string(), - age: number(), - }), - ['age'] -) - -export const data = { - name: 'john', - age: 42, -} - -export const failures = [ - { - value: 42, - type: 'never', - refinement: undefined, - path: ['age'], - branch: [data, data.age], - }, -] diff --git a/test/validation/omit/invalid-property.test.ts b/test/validation/omit/invalid-property.test.ts new file mode 100644 index 00000000..5b5a8cec --- /dev/null +++ b/test/validation/omit/invalid-property.test.ts @@ -0,0 +1,32 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { omit, object, string, number } from '../../../src' + +test('Invalid omit property', () => { + const data = { + age: 'invalid', + } + + const [err, res] = validate( + data, + omit( + object({ + name: string(), + age: number(), + }), + ['name'] + ) + ) + + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 'invalid', + type: 'number', + refinement: undefined, + path: ['age'], + branch: [data, data.age], + }, + ]) +}) diff --git a/test/validation/omit/invalid-property.ts b/test/validation/omit/invalid-property.ts deleted file mode 100644 index ccf7bd11..00000000 --- a/test/validation/omit/invalid-property.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { omit, object, string, number } from '../../../src' - -export const Struct = omit( - object({ - name: string(), - age: number(), - }), - ['name'] -) - -export const data = { - age: 'invalid', -} - -export const failures = [ - { - value: 'invalid', - type: 'number', - refinement: undefined, - path: ['age'], - branch: [data, data.age], - }, -] diff --git a/test/validation/omit/invalid.test.ts b/test/validation/omit/invalid.test.ts new file mode 100644 index 00000000..6ca3fca3 --- /dev/null +++ b/test/validation/omit/invalid.test.ts @@ -0,0 +1,30 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { omit, object, string, number } from '../../../src' + +test('Invalid omit', () => { + const data = 'invalid' + + const [err, res] = validate( + data, + omit( + object({ + name: string(), + age: number(), + }), + ['age'] + ) + ) + + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 'invalid', + type: 'object', + refinement: undefined, + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/omit/invalid.ts b/test/validation/omit/invalid.ts deleted file mode 100644 index fd9acc54..00000000 --- a/test/validation/omit/invalid.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { omit, object, string, number } from '../../../src' - -export const Struct = omit( - object({ - name: string(), - age: number(), - }), - ['age'] -) - -export const data = 'invalid' - -export const failures = [ - { - value: 'invalid', - type: 'object', - refinement: undefined, - path: [], - branch: [data], - }, -] diff --git a/test/validation/omit/valid-nested.test.ts b/test/validation/omit/valid-nested.test.ts new file mode 100644 index 00000000..02e2b1ec --- /dev/null +++ b/test/validation/omit/valid-nested.test.ts @@ -0,0 +1,33 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { omit, object, string } from '../../../src' + +test('Valid omit nested', () => { + const data = { + address: { + street: '123 Fake St', + city: 'Springfield', + }, + } + + assert( + data, + omit( + object({ + name: string(), + address: object({ + street: string(), + city: string(), + }), + }), + ['name'] + ) + ) + + expect(data).toStrictEqual({ + address: { + street: '123 Fake St', + city: 'Springfield', + }, + }) +}) diff --git a/test/validation/omit/valid-nested.ts b/test/validation/omit/valid-nested.ts deleted file mode 100644 index b5e7080a..00000000 --- a/test/validation/omit/valid-nested.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { omit, object, string } from '../../../src' - -export const Struct = omit( - object({ - name: string(), - address: object({ - street: string(), - city: string(), - }), - }), - ['name'] -) - -export const data = { - address: { - street: '123 Fake St', - city: 'Springfield', - }, -} - -export const output = { - address: { - street: '123 Fake St', - city: 'Springfield', - }, -} diff --git a/test/validation/omit/valid-type.test.ts b/test/validation/omit/valid-type.test.ts new file mode 100644 index 00000000..516d0a99 --- /dev/null +++ b/test/validation/omit/valid-type.test.ts @@ -0,0 +1,26 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { omit, type, string, number } from '../../../src' + +test('Valid omit type', () => { + const data = { + name: 'john', + unknownProperty: 'unknown', + } + + assert( + data, + omit( + type({ + name: string(), + age: number(), + }), + ['age'] + ) + ) + + expect(data).toStrictEqual({ + name: 'john', + unknownProperty: 'unknown', + }) +}) diff --git a/test/validation/omit/valid-type.ts b/test/validation/omit/valid-type.ts deleted file mode 100644 index ce7112f4..00000000 --- a/test/validation/omit/valid-type.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { omit, type, string, number } from '../../../src' - -export const Struct = omit( - type({ - name: string(), - age: number(), - }), - ['age'] -) - -export const data = { - name: 'john', - unknownProperty: 'unknown', -} - -export const output = { - name: 'john', - unknownProperty: 'unknown', -} diff --git a/test/validation/omit/valid.test.ts b/test/validation/omit/valid.test.ts new file mode 100644 index 00000000..a23ed0c5 --- /dev/null +++ b/test/validation/omit/valid.test.ts @@ -0,0 +1,24 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { omit, object, string, number } from '../../../src' + +test('Valid omit', () => { + const data = { + name: 'john', + } + + assert( + data, + omit( + object({ + name: string(), + age: number(), + }), + ['age'] + ) + ) + + expect(data).toStrictEqual({ + name: 'john', + }) +}) diff --git a/test/validation/omit/valid.ts b/test/validation/omit/valid.ts deleted file mode 100644 index 45e1e6bf..00000000 --- a/test/validation/omit/valid.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { omit, object, string, number } from '../../../src' - -export const Struct = omit( - object({ - name: string(), - age: number(), - }), - ['age'] -) - -export const data = { - name: 'john', -} - -export const output = { - name: 'john', -} diff --git a/test/validation/optional/invalid.test.ts b/test/validation/optional/invalid.test.ts new file mode 100644 index 00000000..4d5ea6ef --- /dev/null +++ b/test/validation/optional/invalid.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { number, optional } from '../../../src' + +test('Invalid optional', () => { + const data = 'invalid' + const [err, res] = validate(data, optional(number())) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 'invalid', + type: 'number', + refinement: undefined, + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/optional/invalid.ts b/test/validation/optional/invalid.ts deleted file mode 100644 index e94d9d82..00000000 --- a/test/validation/optional/invalid.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { number, optional } from '../../../src' - -export const Struct = optional(number()) - -export const data = 'invalid' - -export const failures = [ - { - value: 'invalid', - type: 'number', - refinement: undefined, - path: [], - branch: [data], - }, -] diff --git a/test/validation/optional/valid-defined-nested.test.ts b/test/validation/optional/valid-defined-nested.test.ts new file mode 100644 index 00000000..9b2250fd --- /dev/null +++ b/test/validation/optional/valid-defined-nested.test.ts @@ -0,0 +1,23 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { type, string, number, optional } from '../../../src' + +test('Valid optional defined nested', () => { + const data = { + name: 'Jill', + age: 42, + } + + assert( + data, + type({ + name: optional(string()), + age: number(), + }) + ) + + expect(data).toStrictEqual({ + name: 'Jill', + age: 42, + }) +}) diff --git a/test/validation/optional/valid-defined-nested.ts b/test/validation/optional/valid-defined-nested.ts deleted file mode 100644 index 69171640..00000000 --- a/test/validation/optional/valid-defined-nested.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { type, string, number, optional } from '../../../src' - -export const Struct = type({ - name: optional(string()), - age: number(), -}) - -export const data = { - name: 'Jill', - age: 42, -} - -export const output = { - name: 'Jill', - age: 42, -} diff --git a/test/validation/optional/valid-defined.test.ts b/test/validation/optional/valid-defined.test.ts new file mode 100644 index 00000000..3e871614 --- /dev/null +++ b/test/validation/optional/valid-defined.test.ts @@ -0,0 +1,9 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { number, optional } from '../../../src' + +test('Valid optional defined', () => { + const data = 42 + assert(data, optional(number())) + expect(data).toStrictEqual(42) +}) diff --git a/test/validation/optional/valid-defined.ts b/test/validation/optional/valid-defined.ts deleted file mode 100644 index 79049904..00000000 --- a/test/validation/optional/valid-defined.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { number, optional } from '../../../src' - -export const Struct = optional(number()) - -export const data = 42 - -export const output = 42 diff --git a/test/validation/optional/valid-undefined-nested.test.ts b/test/validation/optional/valid-undefined-nested.test.ts new file mode 100644 index 00000000..01cb566f --- /dev/null +++ b/test/validation/optional/valid-undefined-nested.test.ts @@ -0,0 +1,21 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { type, string, number, optional } from '../../../src' + +test('Valid optional undefined nested', () => { + const data = { + age: 42, + } + + assert( + data, + type({ + name: optional(string()), + age: number(), + }) + ) + + expect(data).toStrictEqual({ + age: 42, + }) +}) diff --git a/test/validation/optional/valid-undefined-nested.ts b/test/validation/optional/valid-undefined-nested.ts deleted file mode 100644 index d692a4ce..00000000 --- a/test/validation/optional/valid-undefined-nested.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { type, string, number, optional } from '../../../src' - -export const Struct = type({ - name: optional(string()), - age: number(), -}) - -export const data = { - age: 42, -} - -export const output = { - age: 42, -} diff --git a/test/validation/optional/valid-undefined.test.ts b/test/validation/optional/valid-undefined.test.ts new file mode 100644 index 00000000..38da0734 --- /dev/null +++ b/test/validation/optional/valid-undefined.test.ts @@ -0,0 +1,9 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { number, optional } from '../../../src' + +test('Valid optional undefined', () => { + const data = undefined + assert(data, optional(number())) + expect(data).toStrictEqual(undefined) +}) diff --git a/test/validation/optional/valid-undefined.ts b/test/validation/optional/valid-undefined.ts deleted file mode 100644 index 02907cfa..00000000 --- a/test/validation/optional/valid-undefined.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { number, optional } from '../../../src' - -export const Struct = optional(number()) - -export const data = undefined - -export const output = undefined diff --git a/test/validation/partial/composed.test.ts b/test/validation/partial/composed.test.ts new file mode 100644 index 00000000..e908dbc0 --- /dev/null +++ b/test/validation/partial/composed.test.ts @@ -0,0 +1,23 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { partial, object, string, number } from '../../../src' + +test('Composed partial', () => { + const data = { + name: 'john', + } + + assert( + data, + partial( + object({ + name: string(), + age: number(), + }) + ) + ) + + expect(data).toStrictEqual({ + name: 'john', + }) +}) diff --git a/test/validation/partial/composed.ts b/test/validation/partial/composed.ts deleted file mode 100644 index ae5e208d..00000000 --- a/test/validation/partial/composed.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { partial, object, string, number } from '../../../src' - -export const Struct = partial( - object({ - name: string(), - age: number(), - }) -) - -export const data = { - name: 'john', -} - -export const output = { - name: 'john', -} diff --git a/test/validation/partial/invalid-property-unknown.test.ts b/test/validation/partial/invalid-property-unknown.test.ts new file mode 100644 index 00000000..7160a95b --- /dev/null +++ b/test/validation/partial/invalid-property-unknown.test.ts @@ -0,0 +1,30 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { partial, string, number } from '../../../src' + +test('Invalid partial property unknown', () => { + const data = { + name: 'john', + unknown: true, + } + + const [err, res] = validate( + data, + partial({ + name: string(), + age: number(), + }) + ) + + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: true, + type: 'never', + refinement: undefined, + path: ['unknown'], + branch: [data, data.unknown], + }, + ]) +}) diff --git a/test/validation/partial/invalid-property-unknown.ts b/test/validation/partial/invalid-property-unknown.ts deleted file mode 100644 index d0063d2d..00000000 --- a/test/validation/partial/invalid-property-unknown.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { partial, string, number } from '../../../src' - -export const Struct = partial({ - name: string(), - age: number(), -}) - -export const data = { - name: 'john', - unknown: true, -} - -export const failures = [ - { - value: true, - type: 'never', - refinement: undefined, - path: ['unknown'], - branch: [data, data.unknown], - }, -] diff --git a/test/validation/partial/invalid-property.test.ts b/test/validation/partial/invalid-property.test.ts new file mode 100644 index 00000000..cb988a12 --- /dev/null +++ b/test/validation/partial/invalid-property.test.ts @@ -0,0 +1,29 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { partial, string, number } from '../../../src' + +test('Invalid partial property', () => { + const data = { + age: 'invalid', + } + + const [err, res] = validate( + data, + partial({ + name: string(), + age: number(), + }) + ) + + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 'invalid', + type: 'number', + refinement: undefined, + path: ['age'], + branch: [data, data.age], + }, + ]) +}) diff --git a/test/validation/partial/invalid-property.ts b/test/validation/partial/invalid-property.ts deleted file mode 100644 index dfe5b391..00000000 --- a/test/validation/partial/invalid-property.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { partial, string, number } from '../../../src' - -export const Struct = partial({ - name: string(), - age: number(), -}) - -export const data = { - age: 'invalid', -} - -export const failures = [ - { - value: 'invalid', - type: 'number', - refinement: undefined, - path: ['age'], - branch: [data, data.age], - }, -] diff --git a/test/validation/partial/invalid.test.ts b/test/validation/partial/invalid.test.ts new file mode 100644 index 00000000..65738584 --- /dev/null +++ b/test/validation/partial/invalid.test.ts @@ -0,0 +1,27 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { partial, string, number } from '../../../src' + +test('Invalid partial', () => { + const data = 'invalid' + + const [err, res] = validate( + data, + partial({ + name: string(), + age: number(), + }) + ) + + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 'invalid', + type: 'object', + refinement: undefined, + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/partial/invalid.ts b/test/validation/partial/invalid.ts deleted file mode 100644 index 58d9a1fd..00000000 --- a/test/validation/partial/invalid.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { partial, string, number } from '../../../src' - -export const Struct = partial({ - name: string(), - age: number(), -}) - -export const data = 'invalid' - -export const failures = [ - { - value: 'invalid', - type: 'object', - refinement: undefined, - path: [], - branch: [data], - }, -] diff --git a/test/validation/partial/valid-empty.test.ts b/test/validation/partial/valid-empty.test.ts new file mode 100644 index 00000000..c5ae1d82 --- /dev/null +++ b/test/validation/partial/valid-empty.test.ts @@ -0,0 +1,17 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { partial, string, number } from '../../../src' + +test('Valid partial empty', () => { + const data = {} + + assert( + data, + partial({ + name: string(), + age: number(), + }) + ) + + expect(data).toStrictEqual({}) +}) diff --git a/test/validation/partial/valid-empty.ts b/test/validation/partial/valid-empty.ts deleted file mode 100644 index d0c47825..00000000 --- a/test/validation/partial/valid-empty.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { partial, string, number } from '../../../src' - -export const Struct = partial({ - name: string(), - age: number(), -}) - -export const data = {} - -export const output = {} diff --git a/test/validation/partial/valid-full.test.ts b/test/validation/partial/valid-full.test.ts new file mode 100644 index 00000000..58a15c4b --- /dev/null +++ b/test/validation/partial/valid-full.test.ts @@ -0,0 +1,23 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { partial, string, number } from '../../../src' + +test('Valid partial full', () => { + const data = { + name: 'john', + age: 42, + } + + assert( + data, + partial({ + name: string(), + age: number(), + }) + ) + + expect(data).toStrictEqual({ + name: 'john', + age: 42, + }) +}) diff --git a/test/validation/partial/valid-full.ts b/test/validation/partial/valid-full.ts deleted file mode 100644 index 0fbf61a2..00000000 --- a/test/validation/partial/valid-full.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { partial, string, number } from '../../../src' - -export const Struct = partial({ - name: string(), - age: number(), -}) - -export const data = { - name: 'john', - age: 42, -} - -export const output = { - name: 'john', - age: 42, -} diff --git a/test/validation/partial/valid-partial.test.ts b/test/validation/partial/valid-partial.test.ts new file mode 100644 index 00000000..bda51d56 --- /dev/null +++ b/test/validation/partial/valid-partial.test.ts @@ -0,0 +1,21 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { partial, string, number } from '../../../src' + +test('Valid partial partial', () => { + const data = { + name: 'john', + } + + assert( + data, + partial({ + name: string(), + age: number(), + }) + ) + + expect(data).toStrictEqual({ + name: 'john', + }) +}) diff --git a/test/validation/partial/valid-partial.ts b/test/validation/partial/valid-partial.ts deleted file mode 100644 index 81d379d1..00000000 --- a/test/validation/partial/valid-partial.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { partial, string, number } from '../../../src' - -export const Struct = partial({ - name: string(), - age: number(), -}) - -export const data = { - name: 'john', -} - -export const output = { - name: 'john', -} diff --git a/test/validation/partial/valid-type.test.ts b/test/validation/partial/valid-type.test.ts new file mode 100644 index 00000000..75c41424 --- /dev/null +++ b/test/validation/partial/valid-type.test.ts @@ -0,0 +1,25 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { number, partial, string, type } from '../../../src' + +test('Valid partial type', () => { + const data = { + name: 'john', + unknownProperty: true, + } + + assert( + data, + partial( + type({ + name: string(), + age: number(), + }) + ) + ) + + expect(data).toStrictEqual({ + name: 'john', + unknownProperty: true, + }) +}) diff --git a/test/validation/partial/valid-type.ts b/test/validation/partial/valid-type.ts deleted file mode 100644 index 51411a70..00000000 --- a/test/validation/partial/valid-type.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { number, partial, string, type } from '../../../src' - -export const Struct = partial( - type({ - name: string(), - age: number(), - }) -) - -export const data = { - name: 'john', - unknownProperty: true, -} - -export const output = { - name: 'john', - unknownProperty: true, -} diff --git a/test/validation/pattern/invalid.test.ts b/test/validation/pattern/invalid.test.ts new file mode 100644 index 00000000..52b5c9d0 --- /dev/null +++ b/test/validation/pattern/invalid.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { string, pattern } from '../../../src' + +test('Invalid pattern', () => { + const data = 'invalid' + const [err, res] = validate(data, pattern(string(), /\d+/)) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 'invalid', + type: 'string', + refinement: 'pattern', + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/pattern/invalid.ts b/test/validation/pattern/invalid.ts deleted file mode 100644 index cfba4d1c..00000000 --- a/test/validation/pattern/invalid.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { string, pattern } from '../../../src' - -export const Struct = pattern(string(), /\d+/) - -export const data = 'invalid' - -export const failures = [ - { - value: 'invalid', - type: 'string', - refinement: 'pattern', - path: [], - branch: [data], - }, -] diff --git a/test/validation/pattern/valid.test.ts b/test/validation/pattern/valid.test.ts new file mode 100644 index 00000000..febae266 --- /dev/null +++ b/test/validation/pattern/valid.test.ts @@ -0,0 +1,9 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { string, pattern } from '../../../src' + +test('Valid pattern', () => { + const data = '123' + assert(data, pattern(string(), /\d+/)) + expect(data).toStrictEqual('123') +}) diff --git a/test/validation/pattern/valid.ts b/test/validation/pattern/valid.ts deleted file mode 100644 index 76181e53..00000000 --- a/test/validation/pattern/valid.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { string, pattern } from '../../../src' - -export const Struct = pattern(string(), /\d+/) - -export const data = '123' - -export const output = '123' diff --git a/test/validation/pick/invalid-element-nested.test.ts b/test/validation/pick/invalid-element-nested.test.ts new file mode 100644 index 00000000..5c2fb725 --- /dev/null +++ b/test/validation/pick/invalid-element-nested.test.ts @@ -0,0 +1,32 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { pick, object, array, string } from '../../../src' + +test('Invalid pick element nested', () => { + const data = { + emails: ['name@example.com', false], + } + + const [err, res] = validate( + data, + pick( + object({ + name: string(), + emails: array(string()), + }), + ['emails'] + ) + ) + + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: false, + type: 'string', + refinement: undefined, + path: ['emails', 1], + branch: [data, data.emails, data.emails[1]], + }, + ]) +}) diff --git a/test/validation/pick/invalid-element-nested.ts b/test/validation/pick/invalid-element-nested.ts deleted file mode 100644 index 86396d15..00000000 --- a/test/validation/pick/invalid-element-nested.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { pick, object, array, string } from '../../../src' - -export const Struct = pick( - object({ - name: string(), - emails: array(string()), - }), - ['emails'] -) - -export const data = { - emails: ['name@example.com', false], -} - -export const failures = [ - { - value: false, - type: 'string', - refinement: undefined, - path: ['emails', 1], - branch: [data, data.emails, data.emails[1]], - }, -] diff --git a/test/validation/pick/invalid-property-nested.test.ts b/test/validation/pick/invalid-property-nested.test.ts new file mode 100644 index 00000000..b75d602a --- /dev/null +++ b/test/validation/pick/invalid-property-nested.test.ts @@ -0,0 +1,38 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { pick, object, string } from '../../../src' + +test('Invalid pick property nested', () => { + const data = { + address: { + street: 123, + city: 'Springfield', + }, + } + + const [err, res] = validate( + data, + pick( + object({ + name: string(), + address: object({ + street: string(), + city: string(), + }), + }), + ['address'] + ) + ) + + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 123, + type: 'string', + refinement: undefined, + path: ['address', 'street'], + branch: [data, data.address, data.address.street], + }, + ]) +}) diff --git a/test/validation/pick/invalid-property-nested.ts b/test/validation/pick/invalid-property-nested.ts deleted file mode 100644 index 17400b43..00000000 --- a/test/validation/pick/invalid-property-nested.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { pick, object, string } from '../../../src' - -export const Struct = pick( - object({ - name: string(), - address: object({ - street: string(), - city: string(), - }), - }), - ['address'] -) - -export const data = { - address: { - street: 123, - city: 'Springfield', - }, -} - -export const failures = [ - { - value: 123, - type: 'string', - refinement: undefined, - path: ['address', 'street'], - branch: [data, data.address, data.address.street], - }, -] diff --git a/test/validation/pick/invalid-property-unknown.test.ts b/test/validation/pick/invalid-property-unknown.test.ts new file mode 100644 index 00000000..f2de7b01 --- /dev/null +++ b/test/validation/pick/invalid-property-unknown.test.ts @@ -0,0 +1,33 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { pick, object, string, number } from '../../../src' + +test('Invalid pick property unknown', () => { + const data = { + name: 'john', + age: 42, + } + + const [err, res] = validate( + data, + pick( + object({ + name: string(), + age: number(), + }), + ['name'] + ) + ) + + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 42, + type: 'never', + refinement: undefined, + path: ['age'], + branch: [data, data.age], + }, + ]) +}) diff --git a/test/validation/pick/invalid-property-unknown.ts b/test/validation/pick/invalid-property-unknown.ts deleted file mode 100644 index 173a7f36..00000000 --- a/test/validation/pick/invalid-property-unknown.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { pick, object, string, number } from '../../../src' - -export const Struct = pick( - object({ - name: string(), - age: number(), - }), - ['name'] -) - -export const data = { - name: 'john', - age: 42, -} - -export const failures = [ - { - value: 42, - type: 'never', - refinement: undefined, - path: ['age'], - branch: [data, data.age], - }, -] diff --git a/test/validation/pick/invalid-property.test.ts b/test/validation/pick/invalid-property.test.ts new file mode 100644 index 00000000..9e047a5c --- /dev/null +++ b/test/validation/pick/invalid-property.test.ts @@ -0,0 +1,32 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { pick, object, string, number } from '../../../src' + +test('Invalid pick property', () => { + const data = { + age: 'invalid', + } + + const [err, res] = validate( + data, + pick( + object({ + name: string(), + age: number(), + }), + ['age'] + ) + ) + + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 'invalid', + type: 'number', + refinement: undefined, + path: ['age'], + branch: [data, data.age], + }, + ]) +}) diff --git a/test/validation/pick/invalid-property.ts b/test/validation/pick/invalid-property.ts deleted file mode 100644 index 0999bc10..00000000 --- a/test/validation/pick/invalid-property.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { pick, object, string, number } from '../../../src' - -export const Struct = pick( - object({ - name: string(), - age: number(), - }), - ['age'] -) - -export const data = { - age: 'invalid', -} - -export const failures = [ - { - value: 'invalid', - type: 'number', - refinement: undefined, - path: ['age'], - branch: [data, data.age], - }, -] diff --git a/test/validation/pick/invalid.test.ts b/test/validation/pick/invalid.test.ts new file mode 100644 index 00000000..06e7474e --- /dev/null +++ b/test/validation/pick/invalid.test.ts @@ -0,0 +1,30 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { pick, object, string, number } from '../../../src' + +test('Invalid pick', () => { + const data = 'invalid' + + const [err, res] = validate( + data, + pick( + object({ + name: string(), + age: number(), + }), + ['name'] + ) + ) + + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 'invalid', + type: 'object', + refinement: undefined, + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/pick/invalid.ts b/test/validation/pick/invalid.ts deleted file mode 100644 index 7376027b..00000000 --- a/test/validation/pick/invalid.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { pick, object, string, number } from '../../../src' - -export const Struct = pick( - object({ - name: string(), - age: number(), - }), - ['name'] -) - -export const data = 'invalid' - -export const failures = [ - { - value: 'invalid', - type: 'object', - refinement: undefined, - path: [], - branch: [data], - }, -] diff --git a/test/validation/pick/valid-nested.test.ts b/test/validation/pick/valid-nested.test.ts new file mode 100644 index 00000000..aec6f47b --- /dev/null +++ b/test/validation/pick/valid-nested.test.ts @@ -0,0 +1,33 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { pick, object, string } from '../../../src' + +test('Valid pick nested', () => { + const data = { + address: { + street: '123 Fake St', + city: 'Springfield', + }, + } + + assert( + data, + pick( + object({ + name: string(), + address: object({ + street: string(), + city: string(), + }), + }), + ['address'] + ) + ) + + expect(data).toStrictEqual({ + address: { + street: '123 Fake St', + city: 'Springfield', + }, + }) +}) diff --git a/test/validation/pick/valid-nested.ts b/test/validation/pick/valid-nested.ts deleted file mode 100644 index 08874b82..00000000 --- a/test/validation/pick/valid-nested.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { pick, object, string } from '../../../src' - -export const Struct = pick( - object({ - name: string(), - address: object({ - street: string(), - city: string(), - }), - }), - ['address'] -) - -export const data = { - address: { - street: '123 Fake St', - city: 'Springfield', - }, -} - -export const output = { - address: { - street: '123 Fake St', - city: 'Springfield', - }, -} diff --git a/test/validation/pick/valid-type.test.ts b/test/validation/pick/valid-type.test.ts new file mode 100644 index 00000000..e8e6b505 --- /dev/null +++ b/test/validation/pick/valid-type.test.ts @@ -0,0 +1,26 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { number, pick, string, type } from '../../../src' + +test('Valid pick type', () => { + const data = { + name: 'john', + unknownProperty: true, + } + + assert( + data, + pick( + type({ + name: string(), + age: number(), + }), + ['name'] + ) + ) + + expect(data).toStrictEqual({ + name: 'john', + unknownProperty: true, + }) +}) diff --git a/test/validation/pick/valid-type.ts b/test/validation/pick/valid-type.ts deleted file mode 100644 index 28f1a330..00000000 --- a/test/validation/pick/valid-type.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { number, pick, string, type } from '../../../src' - -export const Struct = pick( - type({ - name: string(), - age: number(), - }), - ['name'] -) - -export const data = { - name: 'john', - unknownProperty: true, -} - -export const output = { - name: 'john', - unknownProperty: true, -} diff --git a/test/validation/pick/valid.test.ts b/test/validation/pick/valid.test.ts new file mode 100644 index 00000000..9777812e --- /dev/null +++ b/test/validation/pick/valid.test.ts @@ -0,0 +1,24 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { pick, object, string, number } from '../../../src' + +test('Valid pick', () => { + const data = { + name: 'john', + } + + assert( + data, + pick( + object({ + name: string(), + age: number(), + }), + ['name'] + ) + ) + + expect(data).toStrictEqual({ + name: 'john', + }) +}) diff --git a/test/validation/pick/valid.ts b/test/validation/pick/valid.ts deleted file mode 100644 index 64c5a838..00000000 --- a/test/validation/pick/valid.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { pick, object, string, number } from '../../../src' - -export const Struct = pick( - object({ - name: string(), - age: number(), - }), - ['name'] -) - -export const data = { - name: 'john', -} - -export const output = { - name: 'john', -} diff --git a/test/validation/record/invalid-array.test.ts b/test/validation/record/invalid-array.test.ts new file mode 100644 index 00000000..6629c09f --- /dev/null +++ b/test/validation/record/invalid-array.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { record, string, number } from '../../../src' + +test('Invalid record array', () => { + const data: any[] = [] + const [err, res] = validate(data, record(string(), number())) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: [], + type: 'record', + refinement: undefined, + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/record/invalid-array.ts b/test/validation/record/invalid-array.ts deleted file mode 100644 index a1e58fc7..00000000 --- a/test/validation/record/invalid-array.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { record, string, number } from '../../../src' - -export const Struct = record(string(), number()) - -export const data = [] - -export const failures = [ - { - value: [], - type: 'record', - refinement: undefined, - path: [], - branch: [data], - }, -] diff --git a/test/validation/record/invalid-property.test.ts b/test/validation/record/invalid-property.test.ts new file mode 100644 index 00000000..7147f771 --- /dev/null +++ b/test/validation/record/invalid-property.test.ts @@ -0,0 +1,30 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { record, string, number } from '../../../src' + +test('Invalid record property', () => { + const data = { + a: 'a', + b: 'b', + } + + const [err, res] = validate(data, record(string(), number())) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 'a', + type: 'number', + refinement: undefined, + path: ['a'], + branch: [data, data.a], + }, + { + value: 'b', + type: 'number', + refinement: undefined, + path: ['b'], + branch: [data, data.b], + }, + ]) +}) diff --git a/test/validation/record/invalid-property.ts b/test/validation/record/invalid-property.ts deleted file mode 100644 index 226b6df9..00000000 --- a/test/validation/record/invalid-property.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { record, string, number } from '../../../src' - -export const Struct = record(string(), number()) - -export const data = { - a: 'a', - b: 'b', -} - -export const failures = [ - { - value: 'a', - type: 'number', - refinement: undefined, - path: ['a'], - branch: [data, data.a], - }, - { - value: 'b', - type: 'number', - refinement: undefined, - path: ['b'], - branch: [data, data.b], - }, -] diff --git a/test/validation/record/invalid.test.ts b/test/validation/record/invalid.test.ts new file mode 100644 index 00000000..4bcf07b9 --- /dev/null +++ b/test/validation/record/invalid.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { record, string, number } from '../../../src' + +test('Invalid record', () => { + const data = 'invalid' + const [err, res] = validate(data, record(string(), number())) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 'invalid', + type: 'record', + refinement: undefined, + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/record/invalid.ts b/test/validation/record/invalid.ts deleted file mode 100644 index 13d26fc9..00000000 --- a/test/validation/record/invalid.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { record, string, number } from '../../../src' - -export const Struct = record(string(), number()) - -export const data = 'invalid' - -export const failures = [ - { - value: 'invalid', - type: 'record', - refinement: undefined, - path: [], - branch: [data], - }, -] diff --git a/test/validation/record/valid-frozen.test.ts b/test/validation/record/valid-frozen.test.ts new file mode 100644 index 00000000..339e65b5 --- /dev/null +++ b/test/validation/record/valid-frozen.test.ts @@ -0,0 +1,17 @@ +import { create } from '../../../src' +import { expect, test } from 'vitest' +import { record, string, number } from '../../../src' + +test('Valid record frozen', () => { + const data = Object.freeze({ + a: 1, + b: 2, + }) + + const res = create(data, record(string(), number())) + + expect(res).toStrictEqual({ + a: 1, + b: 2, + }) +}) diff --git a/test/validation/record/valid-frozen.ts b/test/validation/record/valid-frozen.ts deleted file mode 100644 index 1bb5bd83..00000000 --- a/test/validation/record/valid-frozen.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { record, string, number } from '../../../src' - -export const Struct = record(string(), number()) - -export const data = Object.freeze({ - a: 1, - b: 2, -}) - -export const output = { - a: 1, - b: 2, -} - -export const create = true diff --git a/test/validation/record/valid.test.ts b/test/validation/record/valid.test.ts new file mode 100644 index 00000000..335a88f7 --- /dev/null +++ b/test/validation/record/valid.test.ts @@ -0,0 +1,17 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { record, string, number } from '../../../src' + +test('Valid record', () => { + const data = { + a: 1, + b: 2, + } + + assert(data, record(string(), number())) + + expect(data).toStrictEqual({ + a: 1, + b: 2, + }) +}) diff --git a/test/validation/record/valid.ts b/test/validation/record/valid.ts deleted file mode 100644 index c036d9f1..00000000 --- a/test/validation/record/valid.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { record, string, number } from '../../../src' - -export const Struct = record(string(), number()) - -export const data = { - a: 1, - b: 2, -} - -export const output = { - a: 1, - b: 2, -} diff --git a/test/validation/refine/invalid-multiple-refinements.test.ts b/test/validation/refine/invalid-multiple-refinements.test.ts new file mode 100644 index 00000000..5bc6e9b1 --- /dev/null +++ b/test/validation/refine/invalid-multiple-refinements.test.ts @@ -0,0 +1,46 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { string, refine, object } from '../../../src' + +const PasswordValidator = refine(string(), 'MinimumLength', (pw) => + pw.length >= 8 ? true : 'required minimum length of 8' +) +const changePasswordStruct = object({ + newPassword: PasswordValidator, + confirmPassword: string(), +}) + +test('Invalid refine multiple refinements', () => { + const data = { + newPassword: '1234567', + confirmPassword: '123456789', + } + + const [err, res] = validate( + data, + refine(changePasswordStruct, 'PasswordsDoNotMatch', (values) => { + return values.newPassword === values.confirmPassword + ? true + : 'Passwords do not match' + }) + ) + + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: data.newPassword, + type: 'string', + refinement: 'MinimumLength', + path: ['newPassword'], + branch: [data, data.newPassword], + }, + { + value: data, + type: 'object', + refinement: 'PasswordsDoNotMatch', + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/refine/invalid-multiple-refinements.ts b/test/validation/refine/invalid-multiple-refinements.ts deleted file mode 100644 index b2ed1933..00000000 --- a/test/validation/refine/invalid-multiple-refinements.ts +++ /dev/null @@ -1,41 +0,0 @@ -import { string, refine, object } from '../../../src' - -const PasswordValidator = refine(string(), 'MinimumLength', (pw) => - pw.length >= 8 ? true : 'required minimum length of 8' -) -const changePasswordStruct = object({ - newPassword: PasswordValidator, - confirmPassword: string(), -}) - -export const Struct = refine( - changePasswordStruct, - 'PasswordsDoNotMatch', - (values) => { - return values.newPassword === values.confirmPassword - ? true - : 'Passwords do not match' - } -) - -export const data = { - newPassword: '1234567', - confirmPassword: '123456789', -} - -export const failures = [ - { - value: data.newPassword, - type: 'string', - refinement: 'MinimumLength', - path: ['newPassword'], - branch: [data, data.newPassword], - }, - { - value: data, - type: 'object', - refinement: 'PasswordsDoNotMatch', - path: [], - branch: [data], - }, -] diff --git a/test/validation/refine/invalid-shorthand.test.ts b/test/validation/refine/invalid-shorthand.test.ts new file mode 100644 index 00000000..612e1e08 --- /dev/null +++ b/test/validation/refine/invalid-shorthand.test.ts @@ -0,0 +1,24 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { number, refine } from '../../../src' + +test('Invalid refine shorthand', () => { + const data = -1 + + const [err, res] = validate( + data, + refine(number(), 'positive', (v) => v > 0 || 'Number was not positive!') + ) + + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: -1, + type: 'number', + refinement: 'positive', + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/refine/invalid-shorthand.ts b/test/validation/refine/invalid-shorthand.ts deleted file mode 100644 index 04b2b1b1..00000000 --- a/test/validation/refine/invalid-shorthand.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { number, refine } from '../../../src' - -export const Struct = refine( - number(), - 'positive', - (v) => v > 0 || 'Number was not positive!' -) - -export const data = -1 - -export const failures = [ - { - value: -1, - type: 'number', - refinement: 'positive', - path: [], - branch: [data], - }, -] diff --git a/test/validation/refine/invalid.test.ts b/test/validation/refine/invalid.test.ts new file mode 100644 index 00000000..7a6d1db6 --- /dev/null +++ b/test/validation/refine/invalid.test.ts @@ -0,0 +1,22 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { string, refine } from '../../../src' + +test('Invalid refine', () => { + const data = 'invalid' + const [err, res] = validate( + data, + refine(string(), 'email', (value) => value.includes('@')) + ) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 'invalid', + type: 'string', + refinement: 'email', + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/refine/invalid.ts b/test/validation/refine/invalid.ts deleted file mode 100644 index b7c09141..00000000 --- a/test/validation/refine/invalid.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { string, refine } from '../../../src' - -export const Struct = refine(string(), 'email', (value) => value.includes('@')) - -export const data = 'invalid' - -export const failures = [ - { - value: 'invalid', - type: 'string', - refinement: 'email', - path: [], - branch: [data], - }, -] diff --git a/test/validation/refine/valid.test.ts b/test/validation/refine/valid.test.ts new file mode 100644 index 00000000..1bac26c0 --- /dev/null +++ b/test/validation/refine/valid.test.ts @@ -0,0 +1,12 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { string, refine } from '../../../src' + +test('Valid refine', () => { + const data = 'name@example.com' + assert( + data, + refine(string(), 'email', (value) => value.includes('@')) + ) + expect(data).toStrictEqual('name@example.com') +}) diff --git a/test/validation/refine/valid.ts b/test/validation/refine/valid.ts deleted file mode 100644 index d9a74c77..00000000 --- a/test/validation/refine/valid.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { string, refine } from '../../../src' - -export const Struct = refine(string(), 'email', (value) => value.includes('@')) - -export const data = 'name@example.com' - -export const output = 'name@example.com' diff --git a/test/validation/regexp/invalid.test.ts b/test/validation/regexp/invalid.test.ts new file mode 100644 index 00000000..e3cc837d --- /dev/null +++ b/test/validation/regexp/invalid.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { regexp } from '../../../src' + +test('Invalid regexp', () => { + const data = 'invalid' + const [err, res] = validate(data, regexp()) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 'invalid', + type: 'regexp', + refinement: undefined, + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/regexp/invalid.ts b/test/validation/regexp/invalid.ts deleted file mode 100644 index d0207cd4..00000000 --- a/test/validation/regexp/invalid.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { regexp } from '../../../src' - -export const Struct = regexp() - -export const data = 'invalid' - -export const failures = [ - { - value: 'invalid', - type: 'regexp', - refinement: undefined, - path: [], - branch: [data], - }, -] diff --git a/test/validation/regexp/valid.test.ts b/test/validation/regexp/valid.test.ts new file mode 100644 index 00000000..569b2fc4 --- /dev/null +++ b/test/validation/regexp/valid.test.ts @@ -0,0 +1,9 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { regexp } from '../../../src' + +test('Valid regexp', () => { + const data = /./ + assert(data, regexp()) + expect(data).toStrictEqual(data) +}) diff --git a/test/validation/regexp/valid.ts b/test/validation/regexp/valid.ts deleted file mode 100644 index 96daf68b..00000000 --- a/test/validation/regexp/valid.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { regexp } from '../../../src' - -export const Struct = regexp() - -export const data = /./ - -export const output = data diff --git a/test/validation/set/invalid-element.test.ts b/test/validation/set/invalid-element.test.ts new file mode 100644 index 00000000..51804b6a --- /dev/null +++ b/test/validation/set/invalid-element.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { set, number } from '../../../src' + +test('Invalid set element', () => { + const data = new Set([1, 'b', 3]) + const [err, res] = validate(data, set(number())) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 'b', + type: 'number', + refinement: undefined, + path: ['b'], + branch: [data, 'b'], + }, + ]) +}) diff --git a/test/validation/set/invalid-element.ts b/test/validation/set/invalid-element.ts deleted file mode 100644 index 2b475cbd..00000000 --- a/test/validation/set/invalid-element.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { set, number } from '../../../src' - -export const Struct = set(number()) - -export const data = new Set([1, 'b', 3]) - -export const failures = [ - { - value: 'b', - type: 'number', - refinement: undefined, - path: ['b'], - branch: [data, 'b'], - }, -] diff --git a/test/validation/set/invalid-opaque.test.ts b/test/validation/set/invalid-opaque.test.ts new file mode 100644 index 00000000..6a312ee7 --- /dev/null +++ b/test/validation/set/invalid-opaque.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { set } from '../../../src' + +test('Invalid set opaque', () => { + const data = 'invalid' + const [err, res] = validate(data, set()) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 'invalid', + type: 'set', + refinement: undefined, + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/set/invalid-opaque.ts b/test/validation/set/invalid-opaque.ts deleted file mode 100644 index fa2e3991..00000000 --- a/test/validation/set/invalid-opaque.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { set } from '../../../src' - -export const Struct = set() - -export const data = 'invalid' - -export const failures = [ - { - value: 'invalid', - type: 'set', - refinement: undefined, - path: [], - branch: [data], - }, -] diff --git a/test/validation/set/invalid.test.ts b/test/validation/set/invalid.test.ts new file mode 100644 index 00000000..eb9840ed --- /dev/null +++ b/test/validation/set/invalid.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { set, number } from '../../../src' + +test('Invalid set', () => { + const data = 'invalid' + const [err, res] = validate(data, set(number())) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 'invalid', + type: 'set', + refinement: undefined, + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/set/invalid.ts b/test/validation/set/invalid.ts deleted file mode 100644 index 68edca5f..00000000 --- a/test/validation/set/invalid.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { set, number } from '../../../src' - -export const Struct = set(number()) - -export const data = 'invalid' - -export const failures = [ - { - value: 'invalid', - type: 'set', - refinement: undefined, - path: [], - branch: [data], - }, -] diff --git a/test/validation/set/valid-opaque.test.ts b/test/validation/set/valid-opaque.test.ts new file mode 100644 index 00000000..f033e00a --- /dev/null +++ b/test/validation/set/valid-opaque.test.ts @@ -0,0 +1,9 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { set } from '../../../src' + +test('Valid set opaque', () => { + const data = new Set(['a', 2, true]) + assert(data, set()) + expect(data).toStrictEqual(new Set(['a', 2, true])) +}) diff --git a/test/validation/set/valid-opaque.ts b/test/validation/set/valid-opaque.ts deleted file mode 100644 index 5722178d..00000000 --- a/test/validation/set/valid-opaque.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { set } from '../../../src' - -export const Struct = set() - -export const data = new Set(['a', 2, true]) - -export const output = new Set(['a', 2, true]) diff --git a/test/validation/set/valid.test.ts b/test/validation/set/valid.test.ts new file mode 100644 index 00000000..9739421e --- /dev/null +++ b/test/validation/set/valid.test.ts @@ -0,0 +1,9 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { set, number } from '../../../src' + +test('Valid set', () => { + const data = new Set([1, 2, 3]) + assert(data, set(number())) + expect(data).toStrictEqual(new Set([1, 2, 3])) +}) diff --git a/test/validation/set/valid.ts b/test/validation/set/valid.ts deleted file mode 100644 index 77ab5978..00000000 --- a/test/validation/set/valid.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { set, number } from '../../../src' - -export const Struct = set(number()) - -export const data = new Set([1, 2, 3]) - -export const output = new Set([1, 2, 3]) diff --git a/test/validation/size/invalid-array.test.ts b/test/validation/size/invalid-array.test.ts new file mode 100644 index 00000000..f6c7f4f1 --- /dev/null +++ b/test/validation/size/invalid-array.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { array, size, number } from '../../../src' + +test('Invalid size array', () => { + const data: any[] = [] + const [err, res] = validate(data, size(array(number()), 1, 5)) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: [], + type: 'array', + refinement: 'size', + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/size/invalid-array.ts b/test/validation/size/invalid-array.ts deleted file mode 100644 index b16d9479..00000000 --- a/test/validation/size/invalid-array.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { array, size, number } from '../../../src' - -export const Struct = size(array(number()), 1, 5) - -export const data = [] - -export const failures = [ - { - value: [], - type: 'array', - refinement: 'size', - path: [], - branch: [data], - }, -] diff --git a/test/validation/size/invalid-map.test.ts b/test/validation/size/invalid-map.test.ts new file mode 100644 index 00000000..95503ca8 --- /dev/null +++ b/test/validation/size/invalid-map.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { map, size, number, string } from '../../../src' + +test('Invalid size map', () => { + const data = new Map() + const [err, res] = validate(data, size(map(number(), string()), 1, 5)) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: data, + type: 'map', + refinement: 'size', + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/size/invalid-map.ts b/test/validation/size/invalid-map.ts deleted file mode 100644 index 9fd5c1d8..00000000 --- a/test/validation/size/invalid-map.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { map, size, number, string } from '../../../src' - -export const Struct = size(map(number(), string()), 1, 5) - -export const data = new Map() - -export const failures = [ - { - value: data, - type: 'map', - refinement: 'size', - path: [], - branch: [data], - }, -] diff --git a/test/validation/size/invalid-number.test.ts b/test/validation/size/invalid-number.test.ts new file mode 100644 index 00000000..4a5e478f --- /dev/null +++ b/test/validation/size/invalid-number.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { number, size } from '../../../src' + +test('Invalid size number', () => { + const data = 0 + const [err, res] = validate(data, size(number(), 1, 5)) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 0, + type: 'number', + refinement: 'size', + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/size/invalid-number.ts b/test/validation/size/invalid-number.ts deleted file mode 100644 index 9cf861c6..00000000 --- a/test/validation/size/invalid-number.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { number, size } from '../../../src' - -export const Struct = size(number(), 1, 5) - -export const data = 0 - -export const failures = [ - { - value: 0, - type: 'number', - refinement: 'size', - path: [], - branch: [data], - }, -] diff --git a/test/validation/size/invalid-set.test.ts b/test/validation/size/invalid-set.test.ts new file mode 100644 index 00000000..ed68e697 --- /dev/null +++ b/test/validation/size/invalid-set.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { set, size, number } from '../../../src' + +test('Invalid size set', () => { + const data = new Set() + const [err, res] = validate(data, size(set(number()), 1, 5)) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: data, + type: 'set', + refinement: 'size', + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/size/invalid-set.ts b/test/validation/size/invalid-set.ts deleted file mode 100644 index 0aab53c0..00000000 --- a/test/validation/size/invalid-set.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { set, size, number } from '../../../src' - -export const Struct = size(set(number()), 1, 5) - -export const data = new Set() - -export const failures = [ - { - value: data, - type: 'set', - refinement: 'size', - path: [], - branch: [data], - }, -] diff --git a/test/validation/size/invalid-string.test.ts b/test/validation/size/invalid-string.test.ts new file mode 100644 index 00000000..959e623d --- /dev/null +++ b/test/validation/size/invalid-string.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { string, size } from '../../../src' + +test('Invalid size string', () => { + const data = '' + const [err, res] = validate(data, size(string(), 1, 5)) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: '', + type: 'string', + refinement: 'size', + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/size/invalid-string.ts b/test/validation/size/invalid-string.ts deleted file mode 100644 index af9b29ca..00000000 --- a/test/validation/size/invalid-string.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { string, size } from '../../../src' - -export const Struct = size(string(), 1, 5) - -export const data = '' - -export const failures = [ - { - value: '', - type: 'string', - refinement: 'size', - path: [], - branch: [data], - }, -] diff --git a/test/validation/size/valid-array.test.ts b/test/validation/size/valid-array.test.ts new file mode 100644 index 00000000..8f7494db --- /dev/null +++ b/test/validation/size/valid-array.test.ts @@ -0,0 +1,9 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { number, array, size } from '../../../src' + +test('Valid size array', () => { + const data = [1, 2, 3] + assert(data, size(array(number()), 1, 5)) + expect(data).toStrictEqual([1, 2, 3]) +}) diff --git a/test/validation/size/valid-array.ts b/test/validation/size/valid-array.ts deleted file mode 100644 index ed20ca2d..00000000 --- a/test/validation/size/valid-array.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { number, array, size } from '../../../src' - -export const Struct = size(array(number()), 1, 5) - -export const data = [1, 2, 3] - -export const output = [1, 2, 3] diff --git a/test/validation/size/valid-exact.test.ts b/test/validation/size/valid-exact.test.ts new file mode 100644 index 00000000..b8b7475b --- /dev/null +++ b/test/validation/size/valid-exact.test.ts @@ -0,0 +1,9 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { string, size } from '../../../src' + +test('Valid size exact', () => { + const data = 'abcd' + assert(data, size(string(), 4)) + expect(data).toStrictEqual('abcd') +}) diff --git a/test/validation/size/valid-exact.ts b/test/validation/size/valid-exact.ts deleted file mode 100644 index 55c9d74f..00000000 --- a/test/validation/size/valid-exact.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { string, size } from '../../../src' - -export const Struct = size(string(), 4) - -export const data = 'abcd' - -export const output = 'abcd' diff --git a/test/validation/size/valid-map.test.ts b/test/validation/size/valid-map.test.ts new file mode 100644 index 00000000..e8561d40 --- /dev/null +++ b/test/validation/size/valid-map.test.ts @@ -0,0 +1,14 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { string, number, map, size } from '../../../src' + +test('Valid size map', () => { + const data = new Map([ + [1, 'a'], + [2, 'b'], + [3, 'c'], + ]) + + assert(data, size(map(number(), string()), 1, 5)) + expect(data).toStrictEqual(data) +}) diff --git a/test/validation/size/valid-map.ts b/test/validation/size/valid-map.ts deleted file mode 100644 index 84e0cbed..00000000 --- a/test/validation/size/valid-map.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { string, number, map, size } from '../../../src' - -export const Struct = size(map(number(), string()), 1, 5) - -export const data = new Map([ - [1, 'a'], - [2, 'b'], - [3, 'c'], -]) - -export const output = data diff --git a/test/validation/size/valid-max-inclusive.test.ts b/test/validation/size/valid-max-inclusive.test.ts new file mode 100644 index 00000000..ecc7db5c --- /dev/null +++ b/test/validation/size/valid-max-inclusive.test.ts @@ -0,0 +1,9 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { string, size } from '../../../src' + +test('Valid size max inclusive', () => { + const data = 'abcde' + assert(data, size(string(), 1, 5)) + expect(data).toStrictEqual('abcde') +}) diff --git a/test/validation/size/valid-max-inclusive.ts b/test/validation/size/valid-max-inclusive.ts deleted file mode 100644 index 1680ff65..00000000 --- a/test/validation/size/valid-max-inclusive.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { string, size } from '../../../src' - -export const Struct = size(string(), 1, 5) - -export const data = 'abcde' - -export const output = 'abcde' diff --git a/test/validation/size/valid-min-inclusive.test.ts b/test/validation/size/valid-min-inclusive.test.ts new file mode 100644 index 00000000..8b24630b --- /dev/null +++ b/test/validation/size/valid-min-inclusive.test.ts @@ -0,0 +1,9 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { string, size } from '../../../src' + +test('Valid size min inclusive', () => { + const data = 'a' + assert(data, size(string(), 1, 5)) + expect(data).toStrictEqual('a') +}) diff --git a/test/validation/size/valid-min-inclusive.ts b/test/validation/size/valid-min-inclusive.ts deleted file mode 100644 index 4492590b..00000000 --- a/test/validation/size/valid-min-inclusive.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { string, size } from '../../../src' - -export const Struct = size(string(), 1, 5) - -export const data = 'a' - -export const output = 'a' diff --git a/test/validation/size/valid-number.test.ts b/test/validation/size/valid-number.test.ts new file mode 100644 index 00000000..53adaf6d --- /dev/null +++ b/test/validation/size/valid-number.test.ts @@ -0,0 +1,9 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { number, size } from '../../../src' + +test('Valid size number', () => { + const data = 3 + assert(data, size(number(), 1, 5)) + expect(data).toStrictEqual(3) +}) diff --git a/test/validation/size/valid-number.ts b/test/validation/size/valid-number.ts deleted file mode 100644 index 9a51e63d..00000000 --- a/test/validation/size/valid-number.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { number, size } from '../../../src' - -export const Struct = size(number(), 1, 5) - -export const data = 3 - -export const output = 3 diff --git a/test/validation/size/valid-set.test.ts b/test/validation/size/valid-set.test.ts new file mode 100644 index 00000000..e303f0d7 --- /dev/null +++ b/test/validation/size/valid-set.test.ts @@ -0,0 +1,9 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { number, set, size } from '../../../src' + +test('Valid size set', () => { + const data = new Set([1, 2, 3]) + assert(data, size(set(number()), 1, 5)) + expect(data).toStrictEqual(data) +}) diff --git a/test/validation/size/valid-set.ts b/test/validation/size/valid-set.ts deleted file mode 100644 index ba2eb6d1..00000000 --- a/test/validation/size/valid-set.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { number, set, size } from '../../../src' - -export const Struct = size(set(number()), 1, 5) - -export const data = new Set([1, 2, 3]) - -export const output = data diff --git a/test/validation/size/valid-string.test.ts b/test/validation/size/valid-string.test.ts new file mode 100644 index 00000000..083875c5 --- /dev/null +++ b/test/validation/size/valid-string.test.ts @@ -0,0 +1,9 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { string, size } from '../../../src' + +test('Valid size string', () => { + const data = 'two' + assert(data, size(string(), 1, 5)) + expect(data).toStrictEqual('two') +}) diff --git a/test/validation/size/valid-string.ts b/test/validation/size/valid-string.ts deleted file mode 100644 index 24f8fcab..00000000 --- a/test/validation/size/valid-string.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { string, size } from '../../../src' - -export const Struct = size(string(), 1, 5) - -export const data = 'two' - -export const output = 'two' diff --git a/test/validation/string/invalid.test.ts b/test/validation/string/invalid.test.ts new file mode 100644 index 00000000..8dca56f8 --- /dev/null +++ b/test/validation/string/invalid.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { string } from '../../../src' + +test('Invalid string', () => { + const data = false + const [err, res] = validate(data, string()) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: false, + type: 'string', + refinement: undefined, + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/string/invalid.ts b/test/validation/string/invalid.ts deleted file mode 100644 index 16634387..00000000 --- a/test/validation/string/invalid.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { string } from '../../../src' - -export const Struct = string() - -export const data = false - -export const failures = [ - { - value: false, - type: 'string', - refinement: undefined, - path: [], - branch: [data], - }, -] diff --git a/test/validation/string/valid.test.ts b/test/validation/string/valid.test.ts new file mode 100644 index 00000000..60f4b2de --- /dev/null +++ b/test/validation/string/valid.test.ts @@ -0,0 +1,9 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { string } from '../../../src' + +test('Valid string', () => { + const data = 'valid' + assert(data, string()) + expect(data).toStrictEqual('valid') +}) diff --git a/test/validation/string/valid.ts b/test/validation/string/valid.ts deleted file mode 100644 index afc40fac..00000000 --- a/test/validation/string/valid.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { string } from '../../../src' - -export const Struct = string() - -export const data = 'valid' - -export const output = 'valid' diff --git a/test/validation/trimmed/invalid.test.ts b/test/validation/trimmed/invalid.test.ts new file mode 100644 index 00000000..2cef8ee9 --- /dev/null +++ b/test/validation/trimmed/invalid.test.ts @@ -0,0 +1,23 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { string, trimmed } from '../../../src' + +test('Invalid trimmed', () => { + const data = false + + const [err, res] = validate(data, trimmed(string()), { + coerce: true, + }) + + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: false, + type: 'string', + refinement: undefined, + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/trimmed/invalid.ts b/test/validation/trimmed/invalid.ts deleted file mode 100644 index 891ff35a..00000000 --- a/test/validation/trimmed/invalid.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { string, trimmed } from '../../../src' - -export const Struct = trimmed(string()) - -export const data = false - -export const failures = [ - { - value: false, - type: 'string', - refinement: undefined, - path: [], - branch: [data], - }, -] - -export const create = true diff --git a/test/validation/trimmed/valid.test.ts b/test/validation/trimmed/valid.test.ts new file mode 100644 index 00000000..6212caf1 --- /dev/null +++ b/test/validation/trimmed/valid.test.ts @@ -0,0 +1,9 @@ +import { create } from '../../../src' +import { expect, test } from 'vitest' +import { string, trimmed } from '../../../src' + +test('Valid trimmed', () => { + const data = ' valid ' + const res = create(data, trimmed(string())) + expect(res).toStrictEqual('valid') +}) diff --git a/test/validation/trimmed/valid.ts b/test/validation/trimmed/valid.ts deleted file mode 100644 index b6815382..00000000 --- a/test/validation/trimmed/valid.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { string, trimmed } from '../../../src' - -export const Struct = trimmed(string()) - -export const data = ' valid ' - -export const output = 'valid' - -export const create = true diff --git a/test/validation/tuple/invalid-element-missing.test.ts b/test/validation/tuple/invalid-element-missing.test.ts new file mode 100644 index 00000000..e4b522e7 --- /dev/null +++ b/test/validation/tuple/invalid-element-missing.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { tuple, string, number } from '../../../src' + +test('Invalid tuple element missing', () => { + const data = ['A'] + const [err, res] = validate(data, tuple([string(), number()])) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: undefined, + type: 'number', + refinement: undefined, + path: [1], + branch: [data, data[1]], + }, + ]) +}) diff --git a/test/validation/tuple/invalid-element-missing.ts b/test/validation/tuple/invalid-element-missing.ts deleted file mode 100644 index 48ca2e3c..00000000 --- a/test/validation/tuple/invalid-element-missing.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { tuple, string, number } from '../../../src' - -export const Struct = tuple([string(), number()]) - -export const data = ['A'] - -export const failures = [ - { - value: undefined, - type: 'number', - refinement: undefined, - path: [1], - branch: [data, data[1]], - }, -] diff --git a/test/validation/tuple/invalid-element-unknown.test.ts b/test/validation/tuple/invalid-element-unknown.test.ts new file mode 100644 index 00000000..e03297c9 --- /dev/null +++ b/test/validation/tuple/invalid-element-unknown.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { tuple, string, number } from '../../../src' + +test('Invalid tuple element unknown', () => { + const data = ['A', 3, 'unknown'] + const [err, res] = validate(data, tuple([string(), number()])) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 'unknown', + type: 'never', + refinement: undefined, + path: [2], + branch: [data, data[2]], + }, + ]) +}) diff --git a/test/validation/tuple/invalid-element-unknown.ts b/test/validation/tuple/invalid-element-unknown.ts deleted file mode 100644 index ab6afcf7..00000000 --- a/test/validation/tuple/invalid-element-unknown.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { tuple, string, number } from '../../../src' - -export const Struct = tuple([string(), number()]) - -export const data = ['A', 3, 'unknown'] - -export const failures = [ - { - value: 'unknown', - type: 'never', - refinement: undefined, - path: [2], - branch: [data, data[2]], - }, -] diff --git a/test/validation/tuple/invalid-element.test.ts b/test/validation/tuple/invalid-element.test.ts new file mode 100644 index 00000000..de6d8d72 --- /dev/null +++ b/test/validation/tuple/invalid-element.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { tuple, string, number } from '../../../src' + +test('Invalid tuple element', () => { + const data = [false, 3] + const [err, res] = validate(data, tuple([string(), number()])) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: false, + type: 'string', + refinement: undefined, + path: [0], + branch: [data, data[0]], + }, + ]) +}) diff --git a/test/validation/tuple/invalid-element.ts b/test/validation/tuple/invalid-element.ts deleted file mode 100644 index 696c43a3..00000000 --- a/test/validation/tuple/invalid-element.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { tuple, string, number } from '../../../src' - -export const Struct = tuple([string(), number()]) - -export const data = [false, 3] - -export const failures = [ - { - value: false, - type: 'string', - refinement: undefined, - path: [0], - branch: [data, data[0]], - }, -] diff --git a/test/validation/tuple/invalid.test.ts b/test/validation/tuple/invalid.test.ts new file mode 100644 index 00000000..f8c1fb07 --- /dev/null +++ b/test/validation/tuple/invalid.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { tuple, string, number } from '../../../src' + +test('Invalid tuple', () => { + const data = 'invalid' + const [err, res] = validate(data, tuple([string(), number()])) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 'invalid', + type: 'tuple', + refinement: undefined, + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/tuple/invalid.ts b/test/validation/tuple/invalid.ts deleted file mode 100644 index f03c711a..00000000 --- a/test/validation/tuple/invalid.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { tuple, string, number } from '../../../src' - -export const Struct = tuple([string(), number()]) - -export const data = 'invalid' - -export const failures = [ - { - value: 'invalid', - type: 'tuple', - refinement: undefined, - path: [], - branch: [data], - }, -] diff --git a/test/validation/tuple/valid-frozen.test.ts b/test/validation/tuple/valid-frozen.test.ts new file mode 100644 index 00000000..83029b5b --- /dev/null +++ b/test/validation/tuple/valid-frozen.test.ts @@ -0,0 +1,9 @@ +import { create } from '../../../src' +import { expect, test } from 'vitest' +import { tuple, string, number } from '../../../src' + +test('Valid tuple frozen', () => { + const data = Object.freeze(['A', 1]) + const res = create(data, tuple([string(), number()])) + expect(res).toStrictEqual(['A', 1]) +}) diff --git a/test/validation/tuple/valid-frozen.ts b/test/validation/tuple/valid-frozen.ts deleted file mode 100644 index b084af38..00000000 --- a/test/validation/tuple/valid-frozen.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { tuple, string, number } from '../../../src' - -export const Struct = tuple([string(), number()]) - -export const data = Object.freeze(['A', 1]) - -export const output = ['A', 1] - -export const create = true diff --git a/test/validation/tuple/valid.test.ts b/test/validation/tuple/valid.test.ts new file mode 100644 index 00000000..3ea445e3 --- /dev/null +++ b/test/validation/tuple/valid.test.ts @@ -0,0 +1,9 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { tuple, string, number } from '../../../src' + +test('Valid tuple', () => { + const data = ['A', 1] + assert(data, tuple([string(), number()])) + expect(data).toStrictEqual(['A', 1]) +}) diff --git a/test/validation/tuple/valid.ts b/test/validation/tuple/valid.ts deleted file mode 100644 index 3c0d8397..00000000 --- a/test/validation/tuple/valid.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { tuple, string, number } from '../../../src' - -export const Struct = tuple([string(), number()]) - -export const data = ['A', 1] - -export const output = ['A', 1] diff --git a/test/validation/type/invalid-array.test.ts b/test/validation/type/invalid-array.test.ts new file mode 100644 index 00000000..79958b80 --- /dev/null +++ b/test/validation/type/invalid-array.test.ts @@ -0,0 +1,19 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { type } from '../../../src' + +test('Invalid type array', () => { + const data: any[] = [] + const [err, res] = validate(data, type({})) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: [], + type: 'type', + refinement: undefined, + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/type/invalid-array.ts b/test/validation/type/invalid-array.ts deleted file mode 100644 index a0ec3972..00000000 --- a/test/validation/type/invalid-array.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { type } from '../../../src' - -export const Struct = type({}) - -export const data = [] - -export const failures = [ - { - value: [], - type: 'type', - refinement: undefined, - path: [], - branch: [data], - }, -] diff --git a/test/validation/type/invalid-property-nested.test.ts b/test/validation/type/invalid-property-nested.test.ts new file mode 100644 index 00000000..ae5e66b7 --- /dev/null +++ b/test/validation/type/invalid-property-nested.test.ts @@ -0,0 +1,32 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { type, string, number } from '../../../src' + +test('Invalid type property nested', () => { + const data = { + id: 1, + } + + const [err, res] = validate( + data, + type({ + id: number(), + person: type({ + name: string(), + age: number(), + }), + }) + ) + + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: undefined, + type: 'type', + refinement: undefined, + path: ['person'], + branch: [data, undefined], + }, + ]) +}) diff --git a/test/validation/type/invalid-property-nested.ts b/test/validation/type/invalid-property-nested.ts deleted file mode 100644 index ee4cd218..00000000 --- a/test/validation/type/invalid-property-nested.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { type, string, number } from '../../../src' - -export const Struct = type({ - id: number(), - person: type({ - name: string(), - age: number(), - }), -}) - -export const data = { - id: 1, -} - -export const failures = [ - { - value: undefined, - type: 'type', - refinement: undefined, - path: ['person'], - branch: [data, undefined], - }, -] diff --git a/test/validation/type/invalid-property.test.ts b/test/validation/type/invalid-property.test.ts new file mode 100644 index 00000000..3cf8b4a0 --- /dev/null +++ b/test/validation/type/invalid-property.test.ts @@ -0,0 +1,30 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { type, string, number } from '../../../src' + +test('Invalid type property', () => { + const data = { + name: 'john', + age: 'invalid', + } + + const [err, res] = validate( + data, + type({ + name: string(), + age: number(), + }) + ) + + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 'invalid', + type: 'number', + refinement: undefined, + path: ['age'], + branch: [data, data.age], + }, + ]) +}) diff --git a/test/validation/type/invalid-property.ts b/test/validation/type/invalid-property.ts deleted file mode 100644 index 21cf52f3..00000000 --- a/test/validation/type/invalid-property.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { type, string, number } from '../../../src' - -export const Struct = type({ - name: string(), - age: number(), -}) - -export const data = { - name: 'john', - age: 'invalid', -} - -export const failures = [ - { - value: 'invalid', - type: 'number', - refinement: undefined, - path: ['age'], - branch: [data, data.age], - }, -] diff --git a/test/validation/type/invalid.test.ts b/test/validation/type/invalid.test.ts new file mode 100644 index 00000000..ebf7a038 --- /dev/null +++ b/test/validation/type/invalid.test.ts @@ -0,0 +1,27 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { type, string, number } from '../../../src' + +test('Invalid type', () => { + const data = 'invalid' + + const [err, res] = validate( + data, + type({ + name: string(), + age: number(), + }) + ) + + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: 'invalid', + type: 'type', + refinement: undefined, + path: [], + branch: [data], + }, + ]) +}) diff --git a/test/validation/type/invalid.ts b/test/validation/type/invalid.ts deleted file mode 100644 index b44b251f..00000000 --- a/test/validation/type/invalid.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { type, string, number } from '../../../src' - -export const Struct = type({ - name: string(), - age: number(), -}) - -export const data = 'invalid' - -export const failures = [ - { - value: 'invalid', - type: 'type', - refinement: undefined, - path: [], - branch: [data], - }, -] diff --git a/test/validation/type/valid-frozen.test.ts b/test/validation/type/valid-frozen.test.ts new file mode 100644 index 00000000..fe22a68b --- /dev/null +++ b/test/validation/type/valid-frozen.test.ts @@ -0,0 +1,23 @@ +import { create } from '../../../src' +import { expect, test } from 'vitest' +import { type, string, number } from '../../../src' + +test('Valid type frozen', () => { + const data = Object.freeze({ + name: 'john', + age: 42, + }) + + const res = create( + data, + type({ + name: string(), + age: number(), + }) + ) + + expect(res).toStrictEqual({ + name: 'john', + age: 42, + }) +}) diff --git a/test/validation/type/valid-frozen.ts b/test/validation/type/valid-frozen.ts deleted file mode 100644 index cdb08dbc..00000000 --- a/test/validation/type/valid-frozen.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { type, string, number } from '../../../src' - -export const Struct = type({ - name: string(), - age: number(), -}) - -export const data = Object.freeze({ - name: 'john', - age: 42, -}) - -export const output = { - name: 'john', - age: 42, -} - -export const create = true diff --git a/test/validation/type/valid-instance.test.ts b/test/validation/type/valid-instance.test.ts new file mode 100644 index 00000000..ea181c17 --- /dev/null +++ b/test/validation/type/valid-instance.test.ts @@ -0,0 +1,24 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { type, string } from '../../../src' + +class Person { + name: string + + constructor(name: string) { + this.name = name + } +} + +test('Valid type', () => { + const data = new Person('john') + + assert( + data, + type({ + name: string(), + }) + ) + + expect(data).toStrictEqual(new Person('john')) +}) diff --git a/test/validation/type/valid-instance.ts b/test/validation/type/valid-instance.ts deleted file mode 100644 index ad586db1..00000000 --- a/test/validation/type/valid-instance.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { type, string } from '../../../src' - -class Person { - name: string - - constructor(name: string) { - this.name = name - } -} - -export const Struct = type({ - name: string(), -}) - -export const data = new Person('john') - -export const output = data diff --git a/test/validation/type/valid.test.ts b/test/validation/type/valid.test.ts new file mode 100644 index 00000000..bc548067 --- /dev/null +++ b/test/validation/type/valid.test.ts @@ -0,0 +1,20 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { type, string, number } from '../../../src' + +test('Valid type', () => { + const data = { + name: 'john', + age: 42, + } + + assert( + data, + type({ + name: string(), + age: number(), + }) + ) + + expect(data).toStrictEqual(data) +}) diff --git a/test/validation/type/valid.ts b/test/validation/type/valid.ts deleted file mode 100644 index 95315fde..00000000 --- a/test/validation/type/valid.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { type, string, number } from '../../../src' - -export const Struct = type({ - name: string(), - age: number(), -}) - -export const data = { - name: 'john', - age: 42, -} - -export const output = data diff --git a/test/validation/union/coercion-object.test.ts b/test/validation/union/coercion-object.test.ts new file mode 100644 index 00000000..ac0d2db7 --- /dev/null +++ b/test/validation/union/coercion-object.test.ts @@ -0,0 +1,12 @@ +import { create } from '../../../src' +import { expect, test } from 'vitest' +import { union, string, number, defaulted, object } from '../../../src' + +const A = string() +const B = object({ a: number(), b: defaulted(number(), 5) }) + +test('Coercion union object', () => { + const data = { a: 5 } + const res = create(data, union([A, B])) + expect(res).toStrictEqual({ a: 5, b: 5 }) +}) diff --git a/test/validation/union/coercion-object.ts b/test/validation/union/coercion-object.ts deleted file mode 100644 index 2d08f7e5..00000000 --- a/test/validation/union/coercion-object.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { union, string, number, defaulted, object } from '../../../src' - -const A = string() -const B = object({ a: number(), b: defaulted(number(), 5) }) - -export const Struct = union([A, B]) - -export const data = { a: 5 } - -export const output = { a: 5, b: 5 } - -export const create = true diff --git a/test/validation/union/coercion-type.test.ts b/test/validation/union/coercion-type.test.ts new file mode 100644 index 00000000..6f776add --- /dev/null +++ b/test/validation/union/coercion-type.test.ts @@ -0,0 +1,12 @@ +import { create } from '../../../src' +import { expect, test } from 'vitest' +import { union, string, number, defaulted, type } from '../../../src' + +const A = string() +const B = type({ a: number(), b: defaulted(number(), 5) }) + +test('Coercion union type', () => { + const data = { a: 5 } + const res = create(data, union([A, B])) + expect(res).toStrictEqual({ a: 5, b: 5 }) +}) diff --git a/test/validation/union/coercion-type.ts b/test/validation/union/coercion-type.ts deleted file mode 100644 index 8dd25517..00000000 --- a/test/validation/union/coercion-type.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { union, string, number, defaulted, type } from '../../../src' - -const A = string() -const B = type({ a: number(), b: defaulted(number(), 5) }) - -export const Struct = union([A, B]) - -export const data = { a: 5 } - -export const output = { a: 5, b: 5 } - -export const create = true diff --git a/test/validation/union/coercion.test.ts b/test/validation/union/coercion.test.ts new file mode 100644 index 00000000..8b7b39b0 --- /dev/null +++ b/test/validation/union/coercion.test.ts @@ -0,0 +1,12 @@ +import { create } from '../../../src' +import { expect, test } from 'vitest' +import { union, string, number, defaulted } from '../../../src' + +const A = defaulted(string(), 'foo') +const B = number() + +test('Coercion union', () => { + const data = undefined + const res = create(data, union([A, B])) + expect(res).toStrictEqual('foo') +}) diff --git a/test/validation/union/coercion.ts b/test/validation/union/coercion.ts deleted file mode 100644 index dd7a108f..00000000 --- a/test/validation/union/coercion.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { union, string, number, defaulted } from '../../../src' - -const A = defaulted(string(), 'foo') -const B = number() - -export const Struct = union([A, B]) - -export const data = undefined - -export const output = 'foo' - -export const create = true diff --git a/test/validation/union/invalid.test.ts b/test/validation/union/invalid.test.ts new file mode 100644 index 00000000..942ffa9e --- /dev/null +++ b/test/validation/union/invalid.test.ts @@ -0,0 +1,39 @@ +import { validate } from '../../../src' +import { expect, test } from 'vitest' +import { type, union, string, number } from '../../../src' + +const A = type({ a: string() }) +const B = type({ b: number() }) + +test('Invalid union', () => { + const data = { + b: 'invalid', + } + + const [err, res] = validate(data, union([A, B])) + expect(res).toBeUndefined() + + expect(err).toMatchStructError([ + { + value: { b: 'invalid' }, + type: 'union', + refinement: undefined, + path: [], + branch: [data], + }, + { + value: undefined, + type: 'string', + refinement: undefined, + path: ['a'], + branch: [data, undefined], + }, + { + value: 'invalid', + type: 'number', + refinement: undefined, + path: ['b'], + branch: [data, data.b], + }, + ]) +}) diff --git a/test/validation/union/invalid.ts b/test/validation/union/invalid.ts deleted file mode 100644 index ddb5bd8d..00000000 --- a/test/validation/union/invalid.ts +++ /dev/null @@ -1,34 +0,0 @@ -import { type, union, string, number } from '../../../src' - -const A = type({ a: string() }) -const B = type({ b: number() }) - -export const Struct = union([A, B]) - -export const data = { - b: 'invalid', -} - -export const failures = [ - { - value: { b: 'invalid' }, - type: 'union', - refinement: undefined, - path: [], - branch: [data], - }, - { - value: undefined, - type: 'string', - refinement: undefined, - path: ['a'], - branch: [data, undefined], - }, - { - value: 'invalid', - type: 'number', - refinement: undefined, - path: ['b'], - branch: [data, data.b], - }, -] diff --git a/test/validation/union/valid.test.ts b/test/validation/union/valid.test.ts new file mode 100644 index 00000000..76964d14 --- /dev/null +++ b/test/validation/union/valid.test.ts @@ -0,0 +1,18 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { type, union, string, number } from '../../../src' + +const A = type({ a: string() }) +const B = type({ b: number() }) + +test('Valid union', () => { + const data = { + a: 'a', + } + + assert(data, union([A, B])) + + expect(data).toStrictEqual({ + a: 'a', + }) +}) diff --git a/test/validation/union/valid.ts b/test/validation/union/valid.ts deleted file mode 100644 index ef84c887..00000000 --- a/test/validation/union/valid.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { type, union, string, number } from '../../../src' - -const A = type({ a: string() }) -const B = type({ b: number() }) - -export const Struct = union([A, B]) - -export const data = { - a: 'a', -} - -export const output = { - a: 'a', -} diff --git a/test/validation/unknown/valid-number.test.ts b/test/validation/unknown/valid-number.test.ts new file mode 100644 index 00000000..56965055 --- /dev/null +++ b/test/validation/unknown/valid-number.test.ts @@ -0,0 +1,9 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { unknown } from '../../../src' + +test('Valid unknown number', () => { + const data = 1 + assert(data, unknown()) + expect(data).toStrictEqual(1) +}) diff --git a/test/validation/unknown/valid-number.ts b/test/validation/unknown/valid-number.ts deleted file mode 100644 index 9238214a..00000000 --- a/test/validation/unknown/valid-number.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { unknown } from '../../../src' - -export const Struct = unknown() - -export const data = 1 - -export const output = 1 diff --git a/test/validation/unknown/valid-string.test.ts b/test/validation/unknown/valid-string.test.ts new file mode 100644 index 00000000..f42be5c2 --- /dev/null +++ b/test/validation/unknown/valid-string.test.ts @@ -0,0 +1,9 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { unknown } from '../../../src' + +test('Valid unknown string', () => { + const data = 'valid' + assert(data, unknown()) + expect(data).toStrictEqual('valid') +}) diff --git a/test/validation/unknown/valid-string.ts b/test/validation/unknown/valid-string.ts deleted file mode 100644 index 7e82b944..00000000 --- a/test/validation/unknown/valid-string.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { unknown } from '../../../src' - -export const Struct = unknown() - -export const data = 'valid' - -export const output = 'valid' diff --git a/test/validation/unknown/valid-undefined.test.ts b/test/validation/unknown/valid-undefined.test.ts new file mode 100644 index 00000000..c30eab7b --- /dev/null +++ b/test/validation/unknown/valid-undefined.test.ts @@ -0,0 +1,9 @@ +import { assert } from '../../../src' +import { expect, test } from 'vitest' +import { unknown } from '../../../src' + +test('Valid unknown undefined', () => { + const data = undefined + assert(data, unknown()) + expect(data).toStrictEqual(undefined) +}) diff --git a/test/validation/unknown/valid-undefined.ts b/test/validation/unknown/valid-undefined.ts deleted file mode 100644 index d90f1439..00000000 --- a/test/validation/unknown/valid-undefined.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { unknown } from '../../../src' - -export const Struct = unknown() - -export const data = undefined - -export const output = undefined diff --git a/transform.cjs b/transform.cjs new file mode 100644 index 00000000..dde47149 --- /dev/null +++ b/transform.cjs @@ -0,0 +1,293 @@ +export default function transformer(file, api) { + const j = api.jscodeshift + const root = j(file.source) + + const variablesToFind = ['Struct', 'data', 'output', 'create', 'failures'] + + let assertOrCreate = 'assert' // Default to "assert" + let dataDeclaration = null + let structExpression = null + let failureValue = null + let createValue = null + let outputValue = null + + // Define import specifiers + const expectSpecifier = j.importSpecifier(j.identifier('expect')) + const itSpecifier = j.importSpecifier(j.identifier('test')) + const validateSpecifier = j.importSpecifier(j.identifier('validate')) + + // Define import declarations + const vitestImportDeclaration = j.importDeclaration( + [expectSpecifier, itSpecifier], + j.literal('vitest') + ) + + // Find existing import declarations + const existingImports = root.find(j.ImportDeclaration) + + // Handle import for vitest + const hasVitestImport = existingImports + .nodes() + .some( + (importDecl) => + importDecl.source.value === 'vitest' && + importDecl.specifiers.some( + (specifier) => + specifier.imported.name === 'expect' || + specifier.imported.name === 'test' + ) + ) + + if (!hasVitestImport) { + // Add the new import declaration for `vitest` to the top of the file + root.find(j.Program).get('body', 0).insertBefore(vitestImportDeclaration) + } + + // Handle import for "../../../src" + const srcImport = existingImports.find(j.ImportDeclaration, { + source: { value: '../../../src' }, + }) + + // Iterate over the identifiers to find the relevant ones + root.find(j.VariableDeclarator).forEach((path) => { + const name = path.node.id.name + + if (variablesToFind.includes(name)) { + if (name === 'create') { + createValue = path.node.init + if ( + createValue && + createValue.type === 'Literal' && + createValue.value === true + ) { + assertOrCreate = 'create' + } + } else if (name === 'data') { + // Save the entire data variable declaration + dataDeclaration = j.variableDeclaration('const', [ + j.variableDeclarator(path.node.id, path.node.init), + ]) + } else if (name === 'Struct') { + structExpression = path.node.init // Use the expression directly + } else if (name === 'failures') { + failureValue = path.node.init // Use the expression directly + } else if (name === 'output') { + outputValue = path.node.init // Use the expression directly + } + } + }) + + if (failureValue !== null) { + if (srcImport.length > 0) { + // Add `validate` to the existing import declaration + srcImport.forEach((importDecl) => { + if ( + !importDecl.node.specifiers.some( + (specifier) => specifier.imported.name === 'validate' + ) + ) { + importDecl.node.specifiers.push(validateSpecifier) + } + }) + } else { + // Optionally handle the case where the import statement does not exist + // Here you can choose to add a new import declaration for `validate` if needed + const newSrcImportDeclaration = j.importDeclaration( + [validateSpecifier], + j.literal('../../../src') + ) + // Add the new import declaration to the top of the file + root.find(j.Program).get('body', 0).insertBefore(newSrcImportDeclaration) + } + } else if (srcImport.length > 0) { + // Add `validate` to the existing import declaration + srcImport.forEach((importDecl) => { + if ( + !importDecl.node.specifiers.some( + (specifier) => specifier.imported.name === assertOrCreate + ) + ) { + importDecl.node.specifiers.push( + j.importSpecifier(j.identifier(assertOrCreate)) + ) + } + }) + } else { + // Optionally handle the case where the import statement does not exist + // Here you can choose to add a new import declaration for `validate` if needed + const newSrcImportDeclaration = j.importDeclaration( + [j.importSpecifier(j.identifier(assertOrCreate))], + j.literal('../../../src') + ) + // Add the new import declaration to the top of the file + root.find(j.Program).get('body', 0).insertBefore(newSrcImportDeclaration) + } + + // Remove the original data declaration from the root + if (dataDeclaration) { + root + .find(j.VariableDeclaration) + .filter((path) => + path.node.declarations.some((decl) => decl.id.name === 'data') + ) + .remove() + } + + // Remove the original Struct declaration from the root + if (createValue) { + root + .find(j.VariableDeclaration) + .filter((path) => + path.node.declarations.some((decl) => decl.id.name === 'create') + ) + .remove() + } + + // Remove the original Struct declaration from the root + if (structExpression) { + root + .find(j.VariableDeclaration) + .filter((path) => + path.node.declarations.some((decl) => decl.id.name === 'Struct') + ) + .remove() + } + + // Remove the original Struct declaration from the root + if (outputValue) { + root + .find(j.VariableDeclaration) + .filter((path) => + path.node.declarations.some((decl) => decl.id.name === 'output') + ) + .remove() + } + + // Remove the original Struct declaration from the root + if (failureValue) { + root + .find(j.VariableDeclaration) + .filter((path) => + path.node.declarations.some((decl) => decl.id.name === 'failures') + ) + .remove() + } + + // Remove empty export statements + root + .find(j.ExportNamedDeclaration) + .filter((path) => path.node.specifiers.length === 0) + .remove() + + const validateOptions = j.objectExpression([ + j.property('init', j.identifier('coerce'), j.literal(true)), + ]) + + // Convert the filename to the test name + const fileName = file.path + .replace(/.*\/|\.ts$/g, '') // Remove the path and file extension + .toLowerCase() + + const path = file.path.split('/') // Extract the folder name + const folderName = path[path.length - 2] + + const testName = fileName.toLowerCase().replace('-', ' ').split(' ') + + let finalTestName = [testName[0], folderName, ...testName.slice(1)].join(' ') + + finalTestName = finalTestName.charAt(0).toUpperCase() + finalTestName.slice(1) + + finalTestName = finalTestName.replace('-', ' ') + + const testFunction = j.callExpression(j.identifier('test'), [ + j.literal(finalTestName), + j.arrowFunctionExpression( + [], + failureValue !== null + ? j.blockStatement([ + dataDeclaration, + j.variableDeclaration('const', [ + j.variableDeclarator( + j.arrayPattern([j.identifier('err'), j.identifier('res')]), + j.callExpression( + j.identifier('validate'), + assertOrCreate === 'create' + ? [j.identifier('data'), structExpression, validateOptions] + : [j.identifier('data'), structExpression] + ) + ), + ]), + j.expressionStatement( + j.callExpression( + j.memberExpression( + j.callExpression(j.identifier('expect'), [ + j.identifier('res'), + ]), + j.identifier('toBeUndefined') + ), + [] + ) + ), + j.expressionStatement( + j.callExpression( + j.memberExpression( + j.callExpression(j.identifier('expect'), [ + j.identifier('err'), + ]), + j.identifier('toMatchStructError') + ), + [failureValue] + ) + ), + ]) + : assertOrCreate === 'create' + ? j.blockStatement([ + dataDeclaration, + j.variableDeclaration('const', [ + j.variableDeclarator( + j.identifier('res'), + j.callExpression(j.identifier(assertOrCreate), [ + j.identifier('data'), + structExpression, + ]) + ), + ]), + j.expressionStatement( + j.callExpression( + j.memberExpression( + j.callExpression(j.identifier('expect'), [ + j.identifier('res'), + ]), + j.identifier('toStrictEqual') + ), + [outputValue] + ) + ), + ]) + : j.blockStatement([ + dataDeclaration, + j.expressionStatement( + j.callExpression(j.identifier(assertOrCreate), [ + j.identifier('data'), + structExpression, + ]) + ), + j.expressionStatement( + j.callExpression( + j.memberExpression( + j.callExpression(j.identifier('expect'), [ + j.identifier('data'), + ]), + j.identifier('toStrictEqual') + ), + [outputValue] + ) + ), + ]) + ), + ]) + + root.get().node.program.body.push(j.expressionStatement(testFunction)) + + return root.toSource() +} diff --git a/vitest.config.ts b/vitest.config.ts new file mode 100644 index 00000000..fa914d2b --- /dev/null +++ b/vitest.config.ts @@ -0,0 +1,5 @@ +import { defineConfig } from 'vitest/config' + +export default defineConfig({ + test: { setupFiles: ['test/matchers.ts'] }, +})