Skip to content

Commit

Permalink
fix: object schema (#15)
Browse files Browse the repository at this point in the history
Co-authored-by: Vladimir Klepov <vklepov@ozon.ru>
  • Loading branch information
thoughtspile and thoughtspile authored Mar 5, 2023
1 parent 69a692a commit 9505749
Show file tree
Hide file tree
Showing 41 changed files with 65 additions and 48 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
"node": "^14 || ^16 || >=18"
},
"scripts": {
"test": "rm -rf dist/tests && tsc -p tests/tsconfig.json && uvu dist/tests '.test.js$'",
"test:types": "tsc && tsc -p tests/tsconfig.dts.json --noEmit",
"test:runtime": "rm -f dist/tests/**/*.{ts,js} && tsc -p tests/tsconfig.json && uvu dist/tests '.test.js$'",
"test": "npm run test:types && npm run test:runtime",
"test:coverage": "c8 --100 --exclude dist/tests npm test",
"lint:all": "prettier --check --loglevel warn .",
"format": "prettier --loglevel silent --write",
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export const record = <Item>(
});

export const object = <T = Record<string, never>>(schema: {
[K in keyof Required<T>]: Cast<T[K]>;
[K in keyof T]-?: Cast<T[K]>;
}) =>
instance(Object).map((raw: any) => {
const res = {} as T;
Expand All @@ -96,7 +96,7 @@ export const object = <T = Record<string, never>>(schema: {
export const objectLoose = <
T extends Record<string, unknown> = Record<string, never>
>(schema: {
[K in keyof Required<T>]: Cast<T[K]>;
[K in keyof T]-?: Cast<T[K]>;
}) =>
instance(Object).map((raw: any) => {
const res = { ...raw };
Expand Down
11 changes: 11 additions & 0 deletions tests/tsconfig.dts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"noEmit": true,
"paths": {
"$lib/*": ["../dist/*"]
},
"incremental": true
},
"extends": "../tsconfig.json",
"include": ["./typings"]
}
7 changes: 6 additions & 1 deletion tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
{
"compilerOptions": {
"module": "esnext"
"module": "esnext",
"paths": {
"$lib/*": ["../src/*"]
},
"incremental": true,
"tsBuildInfoFile": "../dist/test.tsbuildinfo"
},
"extends": "../tsconfig.json",
"include": ["./**/*.ts"]
Expand Down
2 changes: 1 addition & 1 deletion tests/typings/Banditype.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
optional,
set,
unknown,
} from "../../src";
} from "$lib/index.js";
import { expectTypeOf } from "expect-type";

expectTypeOf(array(string())).toEqualTypeOf<Banditype<Array<string>>>();
Expand Down
2 changes: 1 addition & 1 deletion tests/typings/any.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expectTypeOf } from "expect-type";
import { banditype } from "../../src";
import { banditype } from "$lib/index.js";

expectTypeOf(banditype<any>((raw) => raw)).returns.toEqualTypeOf<any>();
2 changes: 1 addition & 1 deletion tests/typings/array.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { array, number, unknown } from "../../src";
import { array, number, unknown } from "$lib/index.js";
import { expectTypeOf } from "expect-type";

expectTypeOf(array(unknown())).returns.toEqualTypeOf<unknown[]>();
Expand Down
2 changes: 1 addition & 1 deletion tests/typings/assign.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { object, number, string } from "../../src";
import { object, number, string } from "$lib/index.js";
import { expectTypeOf } from "expect-type";

const schemaA = { a: number() };
Expand Down
2 changes: 1 addition & 1 deletion tests/typings/boolean.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { boolean } from "../../src";
import { boolean } from "$lib/index.js";
import { expectTypeOf } from "expect-type";

expectTypeOf(boolean()).returns.toEqualTypeOf<boolean>();
2 changes: 1 addition & 1 deletion tests/typings/coerce.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { string } from "../../src";
import { string } from "$lib/index.js";
import { expectTypeOf } from "expect-type";

expectTypeOf(
Expand Down
2 changes: 1 addition & 1 deletion tests/typings/date.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { instance, fail } from "../../src";
import { instance, fail } from "$lib/index.js";
import { expectTypeOf } from "expect-type";

expectTypeOf(
Expand Down
2 changes: 1 addition & 1 deletion tests/typings/defaulted.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { string } from "../../src";
import { string } from "$lib/index.js";
import { expectTypeOf } from "expect-type";

expectTypeOf(string().or(() => "default")).returns.toEqualTypeOf<string>();
2 changes: 1 addition & 1 deletion tests/typings/empty.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { string, array, map, set, fail, unknown } from "../../src";
import { string, array, map, set, fail, unknown } from "$lib/index.js";
import { expectTypeOf } from "expect-type";

expectTypeOf(
Expand Down
2 changes: 1 addition & 1 deletion tests/typings/enums.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { enums } from "../../src";
import { enums } from "$lib/index.js";
import { expectTypeOf } from "expect-type";

expectTypeOf((x) => {
Expand Down
2 changes: 1 addition & 1 deletion tests/typings/func.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { func } from "../../src";
import { func } from "$lib/index.js";
import { expectTypeOf } from "expect-type";

expectTypeOf(func()).returns.toEqualTypeOf<(...args: unknown[]) => unknown>();
2 changes: 1 addition & 1 deletion tests/typings/infer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type Infer, object } from "../../src";
import { type Infer, object } from "$lib/index.js";
import { expectTypeOf } from "expect-type";

const Struct = object({});
Expand Down
2 changes: 1 addition & 1 deletion tests/typings/instance.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { instance } from "../../src";
import { instance } from "$lib/index.js";
import { expectTypeOf } from "expect-type";

expectTypeOf(instance(Date)).returns.toEqualTypeOf<Date>();
Expand Down
2 changes: 1 addition & 1 deletion tests/typings/integer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fail, number } from "../../src";
import { fail, number } from "$lib/index.js";
import { expectTypeOf } from "expect-type";

expectTypeOf(
Expand Down
2 changes: 1 addition & 1 deletion tests/typings/lazy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { lazy, string } from "../../src";
import { lazy, string } from "$lib/index.js";
import { expectTypeOf } from "expect-type";

expectTypeOf(lazy(() => string())).returns.toEqualTypeOf<string>();
2 changes: 1 addition & 1 deletion tests/typings/literal.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { enums } from "../../src";
import { enums } from "$lib/index.js";
import { expectTypeOf } from "expect-type";

expectTypeOf(enums([true])).returns.toEqualTypeOf<true>();
Expand Down
2 changes: 1 addition & 1 deletion tests/typings/map.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { map, string, number } from "../../src";
import { map, string, number } from "$lib/index.js";
import { expectTypeOf } from "expect-type";

expectTypeOf(map(string(), number())).returns.toEqualTypeOf<
Expand Down
2 changes: 1 addition & 1 deletion tests/typings/max.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { number, fail } from "../../src";
import { number, fail } from "$lib/index.js";
import { expectTypeOf } from "expect-type";

expectTypeOf(
Expand Down
2 changes: 1 addition & 1 deletion tests/typings/min.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { number, fail } from "../../src";
import { number, fail } from "$lib/index.js";
import { expectTypeOf } from "expect-type";

expectTypeOf(
Expand Down
2 changes: 1 addition & 1 deletion tests/typings/never.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { never } from "../../src";
import { never } from "$lib/index.js";
import { expectTypeOf } from "expect-type";

expectTypeOf(never()).returns.toEqualTypeOf<never>();
2 changes: 1 addition & 1 deletion tests/typings/nullable.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { string, object, enums } from "../../src";
import { string, object, enums } from "$lib/index.js";
import { expectTypeOf } from "expect-type";

expectTypeOf(string().or(enums([null]))).returns.toEqualTypeOf<string | null>();
Expand Down
2 changes: 1 addition & 1 deletion tests/typings/number.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { number } from "../../src";
import { number } from "$lib/index.js";
import { expectTypeOf } from "expect-type";

expectTypeOf(number()).returns.toEqualTypeOf<number>();
14 changes: 6 additions & 8 deletions tests/typings/object.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { object, number, string, Banditype } from "../../src";
import { object, number, string, Cast } from "$lib/index.js";
import { expectTypeOf } from "expect-type";

expectTypeOf(object({})).returns.toEqualTypeOf<Record<string, never>>();
Expand All @@ -13,10 +13,8 @@ expectTypeOf(
b: string;
}>();

expectTypeOf(
object<{ key?: string }>({
key: string(),
})
).parameters.toEqualTypeOf<{
key: Banditype<string>;
}>;
expectTypeOf(object<{ key?: string }>)
.parameter(0)
.toEqualTypeOf<{
key: Cast<string | undefined>;
}>();
2 changes: 1 addition & 1 deletion tests/typings/objectLoose.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { objectLoose, number } from "../../src";
import { objectLoose, number } from "$lib/index.js";
import { expectTypeOf } from "expect-type";

expectTypeOf(objectLoose({ a: number() })).returns.toEqualTypeOf<{
Expand Down
2 changes: 1 addition & 1 deletion tests/typings/optional.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { optional, string, number, object, enums } from "../../src";
import { optional, string, number, object, enums } from "$lib/index.js";
import { expectTypeOf } from "expect-type";

expectTypeOf(string().or(optional())).returns.toEqualTypeOf<
Expand Down
2 changes: 1 addition & 1 deletion tests/typings/pattern.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fail, string } from "../../src";
import { fail, string } from "$lib/index.js";
import { expectTypeOf } from "expect-type";

expectTypeOf(
Expand Down
2 changes: 1 addition & 1 deletion tests/typings/record.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { record, number } from "../../src";
import { record, number } from "$lib/index.js";
import { expectTypeOf } from "expect-type";

expectTypeOf(record(number())).returns.toEqualTypeOf<Record<string, number>>();
2 changes: 1 addition & 1 deletion tests/typings/refine.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { string } from "../../src";
import { string } from "$lib/index.js";
import { expectTypeOf } from "expect-type";

expectTypeOf(string().map((x) => x)).returns.toEqualTypeOf<string>();
2 changes: 1 addition & 1 deletion tests/typings/regexp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { instance } from "../../src";
import { instance } from "$lib/index.js";
import { expectTypeOf } from "expect-type";

expectTypeOf(instance(RegExp)).returns.toEqualTypeOf<RegExp>();
2 changes: 1 addition & 1 deletion tests/typings/set.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { set, string } from "../../src";
import { set, string } from "$lib/index.js";
import { expectTypeOf } from "expect-type";

expectTypeOf(set(string())).returns.toEqualTypeOf<Set<string>>();
2 changes: 1 addition & 1 deletion tests/typings/string.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { string } from "../../src";
import { string } from "$lib/index.js";
import { expectTypeOf } from "expect-type";

expectTypeOf(string()).returns.toEqualTypeOf<string>();
2 changes: 1 addition & 1 deletion tests/typings/struct.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { banditype } from "../../src";
import { banditype } from "$lib/index.js";
import { expectTypeOf } from "expect-type";

expectTypeOf(
Expand Down
2 changes: 1 addition & 1 deletion tests/typings/trimmed.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { string } from "../../src";
import { string } from "$lib/index.js";
import { expectTypeOf } from "expect-type";

expectTypeOf(string().map((x) => x.trim())).returns.toEqualTypeOf<string>();
2 changes: 1 addition & 1 deletion tests/typings/tuple.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { tuple, string, number, enums } from "../../src";
import { tuple, string, number, enums } from "$lib/index.js";
import { expectTypeOf } from "expect-type";

expectTypeOf(tuple([string(), number()] as const)).returns.toEqualTypeOf<
Expand Down
2 changes: 1 addition & 1 deletion tests/typings/union.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { object, string, enums } from "../../src";
import { object, string, enums } from "$lib/index.js";
import { expectTypeOf } from "expect-type";

expectTypeOf(
Expand Down
2 changes: 1 addition & 1 deletion tests/typings/unknown.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { unknown } from "../../src";
import { unknown } from "$lib/index.js";
import { expectTypeOf } from "expect-type";

expectTypeOf(unknown()).returns.toEqualTypeOf<unknown>();
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"outDir": "dist",
"rootDirs": ["./src"],
"strictNullChecks": true,
"target": "ES2018"
"target": "ES2018",
"incremental": true
},
"include": ["src"]
}

0 comments on commit 9505749

Please sign in to comment.