Skip to content

Commit

Permalink
feat: update types
Browse files Browse the repository at this point in the history
  • Loading branch information
yamiteru committed Apr 16, 2024
1 parent 6849f3d commit 062e06b
Show file tree
Hide file tree
Showing 48 changed files with 224 additions and 215 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand Down
3 changes: 0 additions & 3 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
"suspicious": {
"useValidTypeof": "off",
"noExplicitAny": "off"
},
"complexity": {
"useArrowFunction": "off"
}
}
},
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,26 @@
"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",
"build:types": "tsup",
"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 ./"
},
"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": {
Expand All @@ -61,6 +61,6 @@
},
"dependencies": {
"@the-minimal/error": "0.0.3",
"@the-minimal/types": "0.0.4"
"@the-minimal/types": "0.1.0"
}
}
16 changes: 9 additions & 7 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import type { Assertion, Pretty } from "@the-minimal/types";
import type { Pretty, Validation } from "@the-minimal/types";

export type ObjectUnknown = Record<string | number | symbol, unknown>;

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<string, Assertion<unknown>>;
export type Schema = Record<string, Validation<unknown>>;

export type InferSchema<$Schema extends Schema> = Pretty<{
[$Key in keyof $Schema]: $Schema[$Key] extends Assertion<infer $Value>
[$Key in keyof $Schema]: $Schema[$Key] extends Validation<infer $Value>
? $Value
: never;
}>;
Expand All @@ -22,10 +22,12 @@ export type Intersection<R extends unknown[]> = R extends [infer H, ...infer S]
? T
: R;

export type InferAssertionValues<
$Assertions extends Array<Assertion<unknown>>,
export type InferValidationValues<
$Validations extends Array<Validation<unknown>>,
> = {
[$Key in keyof $Assertions]: $Assertions[$Key] extends Assertion<infer $Value>
[$Key in keyof $Validations]: $Validations[$Key] extends Validation<
infer $Value
>
? $Value
: never;
};
20 changes: 12 additions & 8 deletions src/validators/and/index.ts
Original file line number Diff line number Diff line change
@@ -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<Assertion<unknown>>>(
fns: $Assertions,
): Assertion<Intersection<InferAssertionValues<$Assertions>>> => {
export const and = <$Validations extends Array<Validation<unknown>>>(
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<Intersection<InferValidationValues<$Validations>>>;
};
13 changes: 6 additions & 7 deletions src/validators/and2/index.ts
Original file line number Diff line number Diff line change
@@ -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>;
15 changes: 7 additions & 8 deletions src/validators/and3/index.ts
Original file line number Diff line number Diff line change
@@ -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>;
4 changes: 2 additions & 2 deletions src/validators/any/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import type { Assertion } from "@the-minimal/types";
import type { Validation } from "@the-minimal/types";

export const any: Assertion<any> = () => {};
export const any = (() => {}) as Validation<any>;
11 changes: 5 additions & 6 deletions src/validators/array/index.ts
Original file line number Diff line number Diff line change
@@ -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<Array<$Value>> =>
(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<Array<$Value>>;
3 changes: 1 addition & 2 deletions src/validators/email/index.ts
Original file line number Diff line number Diff line change
@@ -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<string> = regex(REGEX_EMAIL);
export const email = regex(REGEX_EMAIL);
7 changes: 3 additions & 4 deletions src/validators/endsWith/index.ts
Original file line number Diff line number Diff line change
@@ -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<string> =>
assert((v) => (v as string).endsWith(value), "endsWith", value);
export const endsWith = (value: string) =>
validate<string>((v) => (v as string).endsWith(value), "endsWith", value);
11 changes: 5 additions & 6 deletions src/validators/expect/index.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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>;
12 changes: 8 additions & 4 deletions src/validators/includes/index.ts
Original file line number Diff line number Diff line change
@@ -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<string | unknown[]> =>
assert((v) => (v as string | unknown[]).includes(value), "includes", value);
export const includes = (value: string) =>
validate<string | unknown[]>(
(v) => (v as string | unknown[]).includes(value),
"includes",
value,
);
2 changes: 1 addition & 1 deletion src/validators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -41,4 +40,5 @@ export * from "./symbol";
export * from "./tuple";
export * from "./type";
export * from "./unknown";
export * from "./validate";
export * from "./value";
8 changes: 4 additions & 4 deletions src/validators/instance/index.ts
Original file line number Diff line number Diff line change
@@ -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);
6 changes: 3 additions & 3 deletions src/validators/integer/index.ts
Original file line number Diff line number Diff line change
@@ -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<number> = assert(Number.isInteger, "integer");
export const integer = validate<number>(Number.isInteger, "integer");
9 changes: 3 additions & 6 deletions src/validators/isArray/index.ts
Original file line number Diff line number Diff line change
@@ -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<Array<unknown>> = assert(
Array.isArray,
"isArray",
);
export const isArray = validate<Array<unknown>>(Array.isArray, "isArray");
5 changes: 2 additions & 3 deletions src/validators/isObject/index.ts
Original file line number Diff line number Diff line change
@@ -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<ObjectUnknown> = assert(
export const isObject = validate<ObjectUnknown>(
(v) => v !== null && typeof v === "object",
"isObject",
);
12 changes: 6 additions & 6 deletions src/validators/lazy/index.ts
Original file line number Diff line number Diff line change
@@ -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<unknown>>(
fn: (value: unknown) => $Assertion,
export const lazy = <$Validation extends Validation<unknown>>(
fn: (value: unknown) => $Validation,
) =>
((value: unknown) => {
(fn(value) as FakeAssertion)(value);
}) as $Assertion;
(fn(value) as FakeValidation)(value);
}) as $Validation;
4 changes: 2 additions & 2 deletions src/validators/lazy/test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -10,7 +10,7 @@ type User = {
friend?: User;
};

const validator: Assertion<User> = object({
const validator: Validation<User> = object({
name: string,
friend: optional(lazy(() => validator)),
});
Expand Down
12 changes: 8 additions & 4 deletions src/validators/length/index.ts
Original file line number Diff line number Diff line change
@@ -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<string | unknown[]> =>
assert((v) => (v as string | unknown[]).length === value, "length", value);
export const length = (value: number) =>
validate<string | unknown[]>(
(v) => (v as string | unknown[]).length === value,
"length",
value,
);
12 changes: 8 additions & 4 deletions src/validators/maxLength/index.ts
Original file line number Diff line number Diff line change
@@ -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<string | unknown[]> =>
assert((v) => (v as string | unknown[]).length <= value, "maxLength", value);
export const maxLength = (value: number) =>
validate<string | unknown[]>(
(v) => (v as string | unknown[]).length <= value,
"maxLength",
value,
);
8 changes: 4 additions & 4 deletions src/validators/maxValue/index.ts
Original file line number Diff line number Diff line change
@@ -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<any> =>
assert((v) => (v as any) <= value, "maxValue", value);
export const maxValue = (value: number) =>
validate<unknown>((v) => (v as any) <= value, "maxValue", value);
12 changes: 8 additions & 4 deletions src/validators/minLength/index.ts
Original file line number Diff line number Diff line change
@@ -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<string | unknown[]> =>
assert((v) => (v as string | unknown[]).length >= value, "minLength", value);
export const minLength = (value: number) =>
validate<string | unknown[]>(
(v) => (v as string | unknown[]).length >= value,
"minLength",
value,
);
8 changes: 4 additions & 4 deletions src/validators/minValue/index.ts
Original file line number Diff line number Diff line change
@@ -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<any> =>
assert((v) => (v as any) >= value, "minValue", value);
export const minValue = (value: number) =>
validate<unknown>((v) => (v as any) >= value, "minValue", value);
Loading

0 comments on commit 062e06b

Please sign in to comment.