From 062e06bd235abc02400eae5ba720cad33f508fa7 Mon Sep 17 00:00:00 2001 From: Yamiteru Date: Tue, 16 Apr 2024 21:13:07 +0200 Subject: [PATCH] feat: update types --- CHANGELOG.md | 4 ++-- biome.json | 3 --- package.json | 10 +++++----- src/types.ts | 16 +++++++++------- src/validators/and/index.ts | 20 ++++++++++++-------- src/validators/and2/index.ts | 13 ++++++------- src/validators/and3/index.ts | 15 +++++++-------- src/validators/any/index.ts | 4 ++-- src/validators/array/index.ts | 11 +++++------ src/validators/email/index.ts | 3 +-- src/validators/endsWith/index.ts | 7 +++---- src/validators/expect/index.ts | 11 +++++------ src/validators/includes/index.ts | 12 ++++++++---- src/validators/index.ts | 2 +- src/validators/instance/index.ts | 8 ++++---- src/validators/integer/index.ts | 6 +++--- src/validators/isArray/index.ts | 9 +++------ src/validators/isObject/index.ts | 5 ++--- src/validators/lazy/index.ts | 12 ++++++------ src/validators/lazy/test.ts | 4 ++-- src/validators/length/index.ts | 12 ++++++++---- src/validators/maxLength/index.ts | 12 ++++++++---- src/validators/maxValue/index.ts | 8 ++++---- src/validators/minLength/index.ts | 12 ++++++++---- src/validators/minValue/index.ts | 8 ++++---- src/validators/multipleOf/index.ts | 8 ++++---- src/validators/nan/index.ts | 6 +++--- src/validators/notLength/index.ts | 12 ++++++++---- src/validators/notValue/index.ts | 8 ++++---- src/validators/nullable/index.ts | 13 +++++-------- src/validators/nullish/index.ts | 13 +++++-------- src/validators/object/index.ts | 16 ++++++++-------- src/validators/optional/index.ts | 13 +++++-------- src/validators/options/index.ts | 4 ++-- src/validators/or/index.ts | 16 ++++++++-------- src/validators/or2/index.ts | 13 ++++++------- src/validators/or3/index.ts | 15 +++++++-------- src/validators/regex/index.ts | 4 ++-- src/validators/startsWith/index.ts | 8 ++++---- src/validators/tuple/index.ts | 16 ++++++++-------- src/validators/type/index.test-d.ts | 7 +++++++ src/validators/type/index.ts | 8 ++++---- src/validators/type/test.ts | 7 ++++--- src/validators/unknown/index.ts | 4 ++-- src/validators/{assert => validate}/index.md | 0 src/validators/{assert => validate}/index.ts | 10 +++++----- src/validators/{assert => validate}/test.ts | 4 ++-- src/validators/value/index.ts | 7 +++---- 48 files changed, 224 insertions(+), 215 deletions(-) create mode 100644 src/validators/type/index.test-d.ts rename src/validators/{assert => validate}/index.md (100%) rename src/validators/{assert => validate}/index.ts (63%) rename src/validators/{assert => validate}/test.ts (66%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 535e97d..063a78f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog). #### [0.0.3](https://github.com/the-minimal/validator/compare/0.0.2...0.0.3) -- feat: added more assertions [`08c8cea`](https://github.com/the-minimal/validator/commit/08c8cea2ce911c9f95a6e5396dcb5eb1f273a078) +- feat: added more validateions [`08c8cea`](https://github.com/the-minimal/validator/commit/08c8cea2ce911c9f95a6e5396dcb5eb1f273a078) - feat: use @the-minimal/types and @the-minimal/error [`abd8dbd`](https://github.com/the-minimal/validator/commit/abd8dbd560fe9bfa111c45e2b0062608c043ec59) - feat: change error handling [`12e98c8`](https://github.com/the-minimal/validator/commit/12e98c859f320029dd3e1a189e59f0568d2392a9) @@ -15,7 +15,7 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog). > 14 April 2024 - feat: move files into folders [`edffcee`](https://github.com/the-minimal/validator/commit/edffcee73583a1c55f875127c87773fc2a7328b2) -- feat: move assertions into a different folder [`d2c9e61`](https://github.com/the-minimal/validator/commit/d2c9e6176964992496a80b24aae07588c04b301e) +- feat: move validateions into a different folder [`d2c9e61`](https://github.com/the-minimal/validator/commit/d2c9e6176964992496a80b24aae07588c04b301e) - feat: add more validators [`d3d3655`](https://github.com/the-minimal/validator/commit/d3d36553bb4a65dd29314cbd18bf83f7529e0afd) #### 0.0.1 diff --git a/biome.json b/biome.json index ae02895..16cf2a2 100644 --- a/biome.json +++ b/biome.json @@ -11,9 +11,6 @@ "suspicious": { "useValidTypeof": "off", "noExplicitAny": "off" - }, - "complexity": { - "useArrowFunction": "off" } } }, diff --git a/package.json b/package.json index ee1e939..b0fb9b3 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "url": "https://github.com/the-minimal/validator/issues" }, "scripts": { - "prepublishOnly": "bun run check && bun run build && bun run test", + "prepublishOnly": "bun run check && bun run build && bun run test:run", "release": "release-it", "build": "bun run build:imports && bun run build:clean && bun run build:code && bun run build:types && bun run build:stats", "build:code": "bun run scripts/build.ts", @@ -39,7 +39,7 @@ "build:stats": "bun run scripts/stats.ts", "build:clean": "rm -rf build", "build:imports": "bun run scripts/imports.ts", - "test:run": "vitest run", + "test:run": "vitest run --typecheck", "test:watch": "vitest", "test:coverage": "vitest run --coverage", "check": "bunx @biomejs/biome check --apply ./" @@ -47,10 +47,10 @@ "devDependencies": { "@biomejs/biome": "1.6.4", "@types/bun": "latest", - "@vitest/coverage-v8": "^1.5.0", + "@vitest/coverage-v8": "1.5.0", "release-it": "17.2.0", "tsup": "8.0.2", - "vite-tsconfig-paths": "^4.3.2", + "vite-tsconfig-paths": "4.3.2", "vitest": "1.5.0" }, "peerDependencies": { @@ -61,6 +61,6 @@ }, "dependencies": { "@the-minimal/error": "0.0.3", - "@the-minimal/types": "0.0.4" + "@the-minimal/types": "0.1.0" } } diff --git a/src/types.ts b/src/types.ts index a574071..79560fc 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,17 +1,17 @@ -import type { Assertion, Pretty } from "@the-minimal/types"; +import type { Pretty, Validation } from "@the-minimal/types"; export type ObjectUnknown = Record; export type FunctionUnknown = (...args: unknown[]) => unknown; -export type FakeAssertion = (value: unknown) => unknown; +export type FakeValidation = (value: unknown) => unknown; export type Message = (error: any, value: unknown) => string; -export type Schema = Record>; +export type Schema = Record>; export type InferSchema<$Schema extends Schema> = Pretty<{ - [$Key in keyof $Schema]: $Schema[$Key] extends Assertion + [$Key in keyof $Schema]: $Schema[$Key] extends Validation ? $Value : never; }>; @@ -22,10 +22,12 @@ export type Intersection = R extends [infer H, ...infer S] ? T : R; -export type InferAssertionValues< - $Assertions extends Array>, +export type InferValidationValues< + $Validations extends Array>, > = { - [$Key in keyof $Assertions]: $Assertions[$Key] extends Assertion + [$Key in keyof $Validations]: $Validations[$Key] extends Validation< + infer $Value + > ? $Value : never; }; diff --git a/src/validators/and/index.ts b/src/validators/and/index.ts index f253174..3ad7573 100644 --- a/src/validators/and/index.ts +++ b/src/validators/and/index.ts @@ -1,14 +1,18 @@ -import type { Assertion } from "@the-minimal/types"; -import type { FakeAssertion, InferAssertionValues, Intersection } from "@types"; +import type { Validation } from "@the-minimal/types"; +import type { + FakeValidation, + InferValidationValues, + Intersection, +} from "@types"; -export const and = <$Assertions extends Array>>( - fns: $Assertions, -): Assertion>> => { +export const and = <$Validations extends Array>>( + fns: $Validations, +) => { const length = fns.length; - return (value) => { + return ((value) => { for (let i = 0; i < length; ++i) { - (fns[i] as FakeAssertion)(value); + (fns[i] as FakeValidation)(value); } - }; + }) as Validation>>; }; diff --git a/src/validators/and2/index.ts b/src/validators/and2/index.ts index 97f96fb..2ab72fc 100644 --- a/src/validators/and2/index.ts +++ b/src/validators/and2/index.ts @@ -1,11 +1,10 @@ -import type { Assertion } from "@the-minimal/types"; +import type { Validation } from "@the-minimal/types"; export const and2 = <$Value1, $Value2>( - fn1: Assertion<$Value1>, - fn2: Assertion<$Value2>, -): Assertion<$Value1 & $Value2> => { - return (value: unknown) => { + fn1: Validation<$Value1>, + fn2: Validation<$Value2>, +) => + ((value: unknown) => { fn1(value); fn2(value); - }; -}; + }) as Validation<$Value1 & $Value2>; diff --git a/src/validators/and3/index.ts b/src/validators/and3/index.ts index 1990a96..658e621 100644 --- a/src/validators/and3/index.ts +++ b/src/validators/and3/index.ts @@ -1,13 +1,12 @@ -import type { Assertion } from "@the-minimal/types"; +import type { Validation } from "@the-minimal/types"; export const and3 = <$Value1, $Value2, $Value3>( - fn1: Assertion<$Value1>, - fn2: Assertion<$Value2>, - fn3: Assertion<$Value3>, -): Assertion<$Value1 & $Value2 & $Value3> => { - return (value: unknown) => { + fn1: Validation<$Value1>, + fn2: Validation<$Value2>, + fn3: Validation<$Value3>, +) => + ((value: unknown) => { fn1(value); fn2(value); fn3(value); - }; -}; + }) as Validation<$Value1 & $Value2 & $Value3>; diff --git a/src/validators/any/index.ts b/src/validators/any/index.ts index 4ac79ac..568de90 100644 --- a/src/validators/any/index.ts +++ b/src/validators/any/index.ts @@ -1,3 +1,3 @@ -import type { Assertion } from "@the-minimal/types"; +import type { Validation } from "@the-minimal/types"; -export const any: Assertion = () => {}; +export const any = (() => {}) as Validation; diff --git a/src/validators/array/index.ts b/src/validators/array/index.ts index 82ff8a9..822fe99 100644 --- a/src/validators/array/index.ts +++ b/src/validators/array/index.ts @@ -1,12 +1,11 @@ -import type { Assertion } from "@the-minimal/types"; +import type { Validation } from "@the-minimal/types"; import { isArray } from "@validators/isArray"; -export const array = - <$Value>(fn: Assertion<$Value>): Assertion> => - (value) => { +export const array = <$Value>(fn: Validation<$Value>) => + ((value) => { isArray(value); for (let i = 0; i < (value as unknown[]).length; ++i) { - fn(value[i]); + fn((value as any)[i]); } - }; + }) as Validation>; diff --git a/src/validators/email/index.ts b/src/validators/email/index.ts index b95221f..a30afe7 100644 --- a/src/validators/email/index.ts +++ b/src/validators/email/index.ts @@ -1,5 +1,4 @@ import { REGEX_EMAIL } from "@constants"; -import type { Assertion } from "@the-minimal/types"; import { regex } from "@validators/regex"; -export const email: Assertion = regex(REGEX_EMAIL); +export const email = regex(REGEX_EMAIL); diff --git a/src/validators/endsWith/index.ts b/src/validators/endsWith/index.ts index f441390..d27bca3 100644 --- a/src/validators/endsWith/index.ts +++ b/src/validators/endsWith/index.ts @@ -1,5 +1,4 @@ -import type { Assertion } from "@the-minimal/types"; -import { assert } from "@validators/assert"; +import { validate } from "@validators/validate"; -export const endsWith = (value: string): Assertion => - assert((v) => (v as string).endsWith(value), "endsWith", value); +export const endsWith = (value: string) => + validate((v) => (v as string).endsWith(value), "endsWith", value); diff --git a/src/validators/expect/index.ts b/src/validators/expect/index.ts index cb762b4..99f61e8 100644 --- a/src/validators/expect/index.ts +++ b/src/validators/expect/index.ts @@ -1,10 +1,9 @@ import { error, isError } from "@the-minimal/error"; -import type { Assertion } from "@the-minimal/types"; +import type { Validation } from "@the-minimal/types"; import type { Message } from "@types"; -export const expect = - <$Value>(fn: Assertion<$Value>, message: Message): Assertion<$Value> => - (value: unknown) => { +export const expect = <$Value>(fn: Validation<$Value>, message: Message) => + ((value: unknown) => { try { fn(value); } catch (e) { @@ -13,9 +12,9 @@ export const expect = if (e instanceof Error) { error(`unknown:${e.name}`, value, e.stack, msg); } else if (isError(e)) { - error(e.reason, value, e.context, msg); + error((e as any).reason, value, (e as any).context, msg); } else { error("unknown:unknown", value, e, msg); } } - }; + }) as Validation<$Value>; diff --git a/src/validators/includes/index.ts b/src/validators/includes/index.ts index 6cfd463..e295e48 100644 --- a/src/validators/includes/index.ts +++ b/src/validators/includes/index.ts @@ -1,5 +1,9 @@ -import type { Assertion } from "@the-minimal/types"; -import { assert } from "@validators/assert"; +import type { Validation } from "@the-minimal/types"; +import { validate } from "@validators/validate"; -export const includes = (value: string): Assertion => - assert((v) => (v as string | unknown[]).includes(value), "includes", value); +export const includes = (value: string) => + validate( + (v) => (v as string | unknown[]).includes(value), + "includes", + value, + ); diff --git a/src/validators/index.ts b/src/validators/index.ts index 22cb56b..1cd0408 100644 --- a/src/validators/index.ts +++ b/src/validators/index.ts @@ -3,7 +3,6 @@ export * from "./and2"; export * from "./and3"; export * from "./any"; export * from "./array"; -export * from "./assert"; export * from "./bigint"; export * from "./boolean"; export * from "./date"; @@ -41,4 +40,5 @@ export * from "./symbol"; export * from "./tuple"; export * from "./type"; export * from "./unknown"; +export * from "./validate"; export * from "./value"; diff --git a/src/validators/instance/index.ts b/src/validators/instance/index.ts index dba94ce..7e9470c 100644 --- a/src/validators/instance/index.ts +++ b/src/validators/instance/index.ts @@ -1,5 +1,5 @@ -import type { Assertion, Class } from "@the-minimal/types"; -import { assert } from "@validators/assert"; +import type { Class, Validation } from "@the-minimal/types"; +import { validate } from "@validators/validate"; -export const instance = <$Type>(cls: Class<$Type>): Assertion<$Type> => - assert((value) => value instanceof cls, "instance", cls); +export const instance = <$Type>(cls: Class<$Type>) => + validate<$Type>((value) => value instanceof cls, "instance", cls); diff --git a/src/validators/integer/index.ts b/src/validators/integer/index.ts index ef42554..c81b6f7 100644 --- a/src/validators/integer/index.ts +++ b/src/validators/integer/index.ts @@ -1,4 +1,4 @@ -import type { Assertion } from "@the-minimal/types"; -import { assert } from "@validators/assert"; +import type { Validation } from "@the-minimal/types"; +import { validate } from "@validators/validate"; -export const integer: Assertion = assert(Number.isInteger, "integer"); +export const integer = validate(Number.isInteger, "integer"); diff --git a/src/validators/isArray/index.ts b/src/validators/isArray/index.ts index 9a10cff..a9683d3 100644 --- a/src/validators/isArray/index.ts +++ b/src/validators/isArray/index.ts @@ -1,7 +1,4 @@ -import type { Assertion } from "@the-minimal/types"; -import { assert } from "@validators/assert"; +import type { Validation } from "@the-minimal/types"; +import { validate } from "@validators/validate"; -export const isArray: Assertion> = assert( - Array.isArray, - "isArray", -); +export const isArray = validate>(Array.isArray, "isArray"); diff --git a/src/validators/isObject/index.ts b/src/validators/isObject/index.ts index 4ed15e7..f1985bf 100644 --- a/src/validators/isObject/index.ts +++ b/src/validators/isObject/index.ts @@ -1,8 +1,7 @@ -import type { Assertion } from "@the-minimal/types"; import type { ObjectUnknown } from "@types"; -import { assert } from "@validators/assert"; +import { validate } from "@validators/validate"; -export const isObject: Assertion = assert( +export const isObject = validate( (v) => v !== null && typeof v === "object", "isObject", ); diff --git a/src/validators/lazy/index.ts b/src/validators/lazy/index.ts index d2a0acd..e1ca97c 100644 --- a/src/validators/lazy/index.ts +++ b/src/validators/lazy/index.ts @@ -1,9 +1,9 @@ -import type { Assertion } from "@the-minimal/types"; -import type { FakeAssertion } from "@types"; +import type { Validation } from "@the-minimal/types"; +import type { FakeValidation } from "@types"; -export const lazy = <$Assertion extends Assertion>( - fn: (value: unknown) => $Assertion, +export const lazy = <$Validation extends Validation>( + fn: (value: unknown) => $Validation, ) => ((value: unknown) => { - (fn(value) as FakeAssertion)(value); - }) as $Assertion; + (fn(value) as FakeValidation)(value); + }) as $Validation; diff --git a/src/validators/lazy/test.ts b/src/validators/lazy/test.ts index bcd6a32..6915414 100644 --- a/src/validators/lazy/test.ts +++ b/src/validators/lazy/test.ts @@ -1,4 +1,4 @@ -import type { Assertion } from "@the-minimal/types"; +import type { Validation } from "@the-minimal/types"; import { lazy } from "@validators/lazy"; import { object } from "@validators/object"; import { optional } from "@validators/optional"; @@ -10,7 +10,7 @@ type User = { friend?: User; }; -const validator: Assertion = object({ +const validator: Validation = object({ name: string, friend: optional(lazy(() => validator)), }); diff --git a/src/validators/length/index.ts b/src/validators/length/index.ts index 2fd5cdf..c52264c 100644 --- a/src/validators/length/index.ts +++ b/src/validators/length/index.ts @@ -1,5 +1,9 @@ -import type { Assertion } from "@the-minimal/types"; -import { assert } from "@validators/assert"; +import type { Validation } from "@the-minimal/types"; +import { validate } from "@validators/validate"; -export const length = (value: number): Assertion => - assert((v) => (v as string | unknown[]).length === value, "length", value); +export const length = (value: number) => + validate( + (v) => (v as string | unknown[]).length === value, + "length", + value, + ); diff --git a/src/validators/maxLength/index.ts b/src/validators/maxLength/index.ts index a420e88..602aec9 100644 --- a/src/validators/maxLength/index.ts +++ b/src/validators/maxLength/index.ts @@ -1,5 +1,9 @@ -import type { Assertion } from "@the-minimal/types"; -import { assert } from "@validators/assert"; +import type { Validation } from "@the-minimal/types"; +import { validate } from "@validators/validate"; -export const maxLength = (value: number): Assertion => - assert((v) => (v as string | unknown[]).length <= value, "maxLength", value); +export const maxLength = (value: number) => + validate( + (v) => (v as string | unknown[]).length <= value, + "maxLength", + value, + ); diff --git a/src/validators/maxValue/index.ts b/src/validators/maxValue/index.ts index 6b6f15b..479b7cf 100644 --- a/src/validators/maxValue/index.ts +++ b/src/validators/maxValue/index.ts @@ -1,5 +1,5 @@ -import type { Assertion } from "@the-minimal/types"; -import { assert } from "@validators/assert"; +import type { Validation } from "@the-minimal/types"; +import { validate } from "@validators/validate"; -export const maxValue = (value: number): Assertion => - assert((v) => (v as any) <= value, "maxValue", value); +export const maxValue = (value: number) => + validate((v) => (v as any) <= value, "maxValue", value); diff --git a/src/validators/minLength/index.ts b/src/validators/minLength/index.ts index 3228550..97b6f50 100644 --- a/src/validators/minLength/index.ts +++ b/src/validators/minLength/index.ts @@ -1,5 +1,9 @@ -import type { Assertion } from "@the-minimal/types"; -import { assert } from "@validators/assert"; +import type { Validation } from "@the-minimal/types"; +import { validate } from "@validators/validate"; -export const minLength = (value: number): Assertion => - assert((v) => (v as string | unknown[]).length >= value, "minLength", value); +export const minLength = (value: number) => + validate( + (v) => (v as string | unknown[]).length >= value, + "minLength", + value, + ); diff --git a/src/validators/minValue/index.ts b/src/validators/minValue/index.ts index 68b7720..eab3f33 100644 --- a/src/validators/minValue/index.ts +++ b/src/validators/minValue/index.ts @@ -1,5 +1,5 @@ -import type { Assertion } from "@the-minimal/types"; -import { assert } from "@validators/assert"; +import type { Validation } from "@the-minimal/types"; +import { validate } from "@validators/validate"; -export const minValue = (value: number): Assertion => - assert((v) => (v as any) >= value, "minValue", value); +export const minValue = (value: number) => + validate((v) => (v as any) >= value, "minValue", value); diff --git a/src/validators/multipleOf/index.ts b/src/validators/multipleOf/index.ts index 8928be5..9f76e95 100644 --- a/src/validators/multipleOf/index.ts +++ b/src/validators/multipleOf/index.ts @@ -1,5 +1,5 @@ -import type { Assertion } from "@the-minimal/types"; -import { assert } from "@validators/assert"; +import type { Validation } from "@the-minimal/types"; +import { validate } from "@validators/validate"; -export const multipleOf = (value: number): Assertion => - assert((v) => !((v as number) % value), "multipleOf", value); +export const multipleOf = (value: number) => + validate((v) => !((v as number) % value), "multipleOf", value); diff --git a/src/validators/nan/index.ts b/src/validators/nan/index.ts index 2a1856b..26bee1d 100644 --- a/src/validators/nan/index.ts +++ b/src/validators/nan/index.ts @@ -1,4 +1,4 @@ -import type { Assertion } from "@the-minimal/types"; -import { assert } from "@validators/assert"; +import type { Validation } from "@the-minimal/types"; +import { validate } from "@validators/validate"; -export const nan: Assertion = assert(Number.isNaN, "nan"); +export const nan = validate(Number.isNaN, "nan"); diff --git a/src/validators/notLength/index.ts b/src/validators/notLength/index.ts index 0f0d90d..fce6df7 100644 --- a/src/validators/notLength/index.ts +++ b/src/validators/notLength/index.ts @@ -1,5 +1,9 @@ -import type { Assertion } from "@the-minimal/types"; -import { assert } from "@validators/assert"; +import type { Validation } from "@the-minimal/types"; +import { validate } from "@validators/validate"; -export const notLength = (value: number): Assertion => - assert((v) => (v as string | unknown[]).length !== value, "notLength", value); +export const notLength = (value: number) => + validate( + (v) => (v as string | unknown[]).length !== value, + "notLength", + value, + ); diff --git a/src/validators/notValue/index.ts b/src/validators/notValue/index.ts index 90c69aa..0fca9c2 100644 --- a/src/validators/notValue/index.ts +++ b/src/validators/notValue/index.ts @@ -1,5 +1,5 @@ -import type { Assertion } from "@the-minimal/types"; -import { assert } from "@validators/assert"; +import type { Validation } from "@the-minimal/types"; +import { validate } from "@validators/validate"; -export const notValue = (value: number): Assertion => - assert((v) => (v as any) !== value, "notValue", value); +export const notValue = (value: number) => + validate((v) => (v as any) !== value, "notValue", value); diff --git a/src/validators/nullable/index.ts b/src/validators/nullable/index.ts index ee53a35..a5cf63f 100644 --- a/src/validators/nullable/index.ts +++ b/src/validators/nullable/index.ts @@ -1,15 +1,12 @@ import { ERROR_PREFIX } from "@constants"; import { error } from "@the-minimal/error"; -import type { Assertion, Nullable } from "@the-minimal/types"; +import type { Nullable, Validation } from "@the-minimal/types"; -export const nullable = <$Value>( - assertion: Assertion<$Value>, -): Assertion> => { - return (value: unknown) => { +export const nullable = <$Value>(validation: Validation<$Value>) => + ((value: unknown) => { try { - assertion(value); + validation(value); } catch { value !== null && error(`${ERROR_PREFIX}:nullable`, value); } - }; -}; + }) as Validation>; diff --git a/src/validators/nullish/index.ts b/src/validators/nullish/index.ts index f43b11f..c6adc43 100644 --- a/src/validators/nullish/index.ts +++ b/src/validators/nullish/index.ts @@ -1,17 +1,14 @@ import { ERROR_PREFIX } from "@constants"; import { error } from "@the-minimal/error"; -import type { Assertion, Nullish } from "@the-minimal/types"; +import type { Nullish, Validation } from "@the-minimal/types"; -export const nullish = <$Value>( - assertion: Assertion<$Value>, -): Assertion> => { - return (value) => { +export const nullish = <$Value>(validation: Validation<$Value>) => + ((value) => { try { - assertion(value); + validation(value); } catch { value !== null && value !== undefined && error(`${ERROR_PREFIX}:nullish`, value); } - }; -}; + }) as Validation>; diff --git a/src/validators/object/index.ts b/src/validators/object/index.ts index ffd8ada..ccc5601 100644 --- a/src/validators/object/index.ts +++ b/src/validators/object/index.ts @@ -1,18 +1,18 @@ -import type { Assertion } from "@the-minimal/types"; -import type { FakeAssertion, InferSchema, Schema } from "@types"; +import type { Validation } from "@the-minimal/types"; +import type { FakeValidation, InferSchema, Schema } from "@types"; import { isObject } from "@validators/isObject"; -export const object = <$Schema extends Schema>( - schema: $Schema, -): Assertion> => { +export const object = <$Schema extends Schema>(schema: $Schema) => { const keys = Object.keys(schema); const length = keys.length; - return (value) => { + return ((value) => { isObject(value); for (let i = 0; i < length; ++i) { - (schema as Record)[keys[i]](value[keys[i] as any]); + (schema as Record)[keys[i]]( + (value as any)[keys[i] as any], + ); } - }; + }) as Validation>; }; diff --git a/src/validators/optional/index.ts b/src/validators/optional/index.ts index a6371ee..7aee427 100644 --- a/src/validators/optional/index.ts +++ b/src/validators/optional/index.ts @@ -1,15 +1,12 @@ import { ERROR_PREFIX } from "@constants"; import { error } from "@the-minimal/error"; -import type { Assertion, Optional } from "@the-minimal/types"; +import type { Optional, Validation } from "@the-minimal/types"; -export const optional = <$Value>( - assertion: Assertion<$Value>, -): Assertion> => { - return (value) => { +export const optional = <$Value>(validation: Validation<$Value>) => + ((value) => { try { - assertion(value); + validation(value); } catch { value !== undefined && error(`${ERROR_PREFIX}:optional`, value); } - }; -}; + }) as Validation>; diff --git a/src/validators/options/index.ts b/src/validators/options/index.ts index e55e9f9..465f4a5 100644 --- a/src/validators/options/index.ts +++ b/src/validators/options/index.ts @@ -1,4 +1,4 @@ -import { assert } from "@validators/assert"; +import { validate } from "@validators/validate"; export const options = <$Options extends unknown[]>(options: $Options) => - assert((v) => options.includes(v), "options", options); + validate<$Options[number]>((v) => options.includes(v), "options", options); diff --git a/src/validators/or/index.ts b/src/validators/or/index.ts index 8fa1af7..f1de00a 100644 --- a/src/validators/or/index.ts +++ b/src/validators/or/index.ts @@ -1,21 +1,21 @@ import { ERROR_PREFIX } from "@constants"; import { error } from "@the-minimal/error"; -import type { Assertion } from "@the-minimal/types"; -import type { FakeAssertion } from "@types"; +import type { Validation } from "@the-minimal/types"; +import type { FakeValidation } from "@types"; -export const or = <$Assertions extends Array>>( - fns: $Assertions, -): Assertion<$Assertions[number]> => { +export const or = <$Validations extends Array>>( + fns: $Validations, +) => { const length = fns.length; - return (value) => { + return ((value) => { for (let i = 0; i < length; ++i) { try { - (fns[i] as FakeAssertion)(value); + (fns[i] as FakeValidation)(value); return; } catch {} } error(`${ERROR_PREFIX}:or`, value); - }; + }) as Validation<$Validations[number]>; }; diff --git a/src/validators/or2/index.ts b/src/validators/or2/index.ts index a406fd3..2cdb877 100644 --- a/src/validators/or2/index.ts +++ b/src/validators/or2/index.ts @@ -1,14 +1,13 @@ -import type { Assertion } from "@the-minimal/types"; +import type { Validation } from "@the-minimal/types"; export const or2 = <$Value1, $Value2>( - fn1: Assertion<$Value1>, - fn2: Assertion<$Value2>, -): Assertion<$Value1 | $Value2> => { - return (value: unknown) => { + fn1: Validation<$Value1>, + fn2: Validation<$Value2>, +) => + ((value: unknown) => { try { fn1(value); } catch { fn2(value); } - }; -}; + }) as Validation<$Value1 | $Value2>; diff --git a/src/validators/or3/index.ts b/src/validators/or3/index.ts index ad9f41f..1bbc9a0 100644 --- a/src/validators/or3/index.ts +++ b/src/validators/or3/index.ts @@ -1,11 +1,11 @@ -import type { Assertion } from "@the-minimal/types"; +import type { Validation } from "@the-minimal/types"; export const or3 = <$Value1, $Value2, $Value3>( - fn1: Assertion<$Value1>, - fn2: Assertion<$Value2>, - fn3: Assertion<$Value3>, -): Assertion<$Value1 | $Value2> => { - return (value: unknown) => { + fn1: Validation<$Value1>, + fn2: Validation<$Value2>, + fn3: Validation<$Value3>, +) => + ((value: unknown) => { try { fn1(value); } catch { @@ -15,5 +15,4 @@ export const or3 = <$Value1, $Value2, $Value3>( fn3(value); } } - }; -}; + }) as Validation<$Value1 | $Value2 | $Value3>; diff --git a/src/validators/regex/index.ts b/src/validators/regex/index.ts index c5e4ba1..bb454c4 100644 --- a/src/validators/regex/index.ts +++ b/src/validators/regex/index.ts @@ -1,4 +1,4 @@ -import { assert } from "@validators/assert"; +import { validate } from "@validators/validate"; export const regex = (pattern: RegExp) => - assert((v) => pattern.test(v as any), "regex", pattern); + validate((v) => pattern.test(v as any), "regex", pattern); diff --git a/src/validators/startsWith/index.ts b/src/validators/startsWith/index.ts index 9853c15..c105bef 100644 --- a/src/validators/startsWith/index.ts +++ b/src/validators/startsWith/index.ts @@ -1,5 +1,5 @@ -import type { Assertion } from "@the-minimal/types"; -import { assert } from "@validators/assert"; +import type { Validation } from "@the-minimal/types"; +import { validate } from "@validators/validate"; -export const startsWith = (value: string): Assertion => - assert((v) => (v as string).startsWith(value), "startsWith", value); +export const startsWith = (value: string) => + validate((v) => (v as string).startsWith(value), "startsWith", value); diff --git a/src/validators/tuple/index.ts b/src/validators/tuple/index.ts index 5c2f2a2..76afc98 100644 --- a/src/validators/tuple/index.ts +++ b/src/validators/tuple/index.ts @@ -1,20 +1,20 @@ -import type { Assertion } from "@the-minimal/types"; -import type { FakeAssertion, InferAssertionValues } from "@types"; +import type { Validation } from "@the-minimal/types"; +import type { FakeValidation, InferValidationValues } from "@types"; import { isArray } from "@validators/isArray"; import { length } from "@validators/length"; -export const tuple = <$Tuple extends Array>>( +export const tuple = <$Tuple extends Array>>( tuple: $Tuple, -): Assertion> => { +) => { const l = tuple.length; - const tupleLength: Assertion = length(l); + const tupleLength = length(l); - return (value) => { + return ((value) => { isArray(value); tupleLength(value); for (let i = 0; i < l; ++i) { - (tuple[i] as FakeAssertion)((value as unknown[])[i]); + (tuple[i] as FakeValidation)((value as unknown[])[i]); } - }; + }) as Validation>; }; diff --git a/src/validators/type/index.test-d.ts b/src/validators/type/index.test-d.ts new file mode 100644 index 0000000..515ec0e --- /dev/null +++ b/src/validators/type/index.test-d.ts @@ -0,0 +1,7 @@ +import type { Validation } from "@the-minimal/types"; +import { type } from "@validators/type"; +import { assertType, it } from "vitest"; + +it("should not throw", () => { + assertType>(type("string")); +}); diff --git a/src/validators/type/index.ts b/src/validators/type/index.ts index 5c841ff..6d02e98 100644 --- a/src/validators/type/index.ts +++ b/src/validators/type/index.ts @@ -1,5 +1,5 @@ -import type { Assertion } from "@the-minimal/types"; -import { assert } from "@validators/assert"; +import type { Validation } from "@the-minimal/types"; +import { validate } from "@validators/validate"; -export const type = <$Type>(type: string): Assertion<$Type> => - assert((value) => typeof value === type, "type", type); +export const type = <$Type>(type: string): Validation<$Type> => + validate((value) => typeof value === type, "type", type); diff --git a/src/validators/type/test.ts b/src/validators/type/test.ts index 8e8566f..af7858f 100644 --- a/src/validators/type/test.ts +++ b/src/validators/type/test.ts @@ -1,10 +1,11 @@ +import { Validation } from "@the-minimal/types"; import { type } from "@validators/type"; -import { expect, it } from "vitest"; +import { expect, expectTypeOf, it } from "vitest"; -it("should not throw on valid types", () => { +it("should not throw", () => { expect(() => type("string")("a")).not.toThrow(); }); -it("should throw on invalid types", () => { +it("should throw", () => { expect(() => type("string")({})).toThrow(); }); diff --git a/src/validators/unknown/index.ts b/src/validators/unknown/index.ts index 3d8737a..89146df 100644 --- a/src/validators/unknown/index.ts +++ b/src/validators/unknown/index.ts @@ -1,4 +1,4 @@ -import type { Assertion } from "@the-minimal/types"; +import type { Validation } from "@the-minimal/types"; import { any } from "@validators/any"; -export const unknown: Assertion = any; +export const unknown = any as Validation; diff --git a/src/validators/assert/index.md b/src/validators/validate/index.md similarity index 100% rename from src/validators/assert/index.md rename to src/validators/validate/index.md diff --git a/src/validators/assert/index.ts b/src/validators/validate/index.ts similarity index 63% rename from src/validators/assert/index.ts rename to src/validators/validate/index.ts index 71373f4..695b832 100644 --- a/src/validators/assert/index.ts +++ b/src/validators/validate/index.ts @@ -1,14 +1,14 @@ import { ERROR_PREFIX } from "@constants"; import type { Context, Message } from "@the-minimal/error"; import { error } from "@the-minimal/error"; -import type { Assertion, Predicate } from "@the-minimal/types"; +import type { Predicate, Validation } from "@the-minimal/types"; -export const assert = <$Type>( +export const validate = <$Type>( fn: Predicate, reason: string, context: Context = null, message: Message = null, -): Assertion<$Type> => - function (value: unknown): asserts value is $Type { +) => + ((value: unknown) => { !fn(value) && error(`${ERROR_PREFIX}:${reason}`, value, context, message); - }; + }) as Validation<$Type>; diff --git a/src/validators/assert/test.ts b/src/validators/validate/test.ts similarity index 66% rename from src/validators/assert/test.ts rename to src/validators/validate/test.ts index d142725..be907a1 100644 --- a/src/validators/assert/test.ts +++ b/src/validators/validate/test.ts @@ -1,7 +1,7 @@ import { expect, it } from "vitest"; -import { assert } from "./index"; +import { validate } from "./index"; -const validator = assert((value) => value === 1, "test"); +const validator = validate((value) => value === 1, "test"); it("should not throw", () => { expect(() => validator(1)).not.toThrow(); diff --git a/src/validators/value/index.ts b/src/validators/value/index.ts index 1caaac1..070093a 100644 --- a/src/validators/value/index.ts +++ b/src/validators/value/index.ts @@ -1,5 +1,4 @@ -import type { Assertion } from "@the-minimal/types"; -import { assert } from "@validators/assert"; +import { validate } from "@validators/validate"; -export const value = (value: number): Assertion => - assert((v) => (v as any) === value, "value", value); +export const value = (value: number) => + validate((v) => (v as any) === value, "value", value);