is-kit 1.8.0 → 1.8.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -2
- package/dist/index.d.mts +16 -13
- package/dist/index.d.ts +16 -13
- package/dist/index.js +3 -1
- package/dist/index.mjs +3 -1
- package/package.json +10 -9
package/README.md
CHANGED
|
@@ -186,7 +186,9 @@ const isConfig = struct({
|
|
|
186
186
|
|
|
187
187
|
`optionalKey(guard)` means the property may be missing.
|
|
188
188
|
|
|
189
|
-
Use `struct(schema, { exact: true })` when extra
|
|
189
|
+
Use `struct(schema, { exact: true })` when extra own enumerable string keys
|
|
190
|
+
should be rejected. Exact mode follows `Object.keys(...)` semantics, so symbol
|
|
191
|
+
keys and non-enumerable properties are outside its key matching.
|
|
190
192
|
|
|
191
193
|
If the property must exist but the value may be `undefined`, use `optional(guard)` instead.
|
|
192
194
|
|
|
@@ -312,7 +314,13 @@ Here are the kinds of problems `is-kit` is especially good at solving:
|
|
|
312
314
|
### Typed object guard checks
|
|
313
315
|
|
|
314
316
|
```ts
|
|
315
|
-
import {
|
|
317
|
+
import {
|
|
318
|
+
isNumber,
|
|
319
|
+
isString,
|
|
320
|
+
optionalKey,
|
|
321
|
+
safeParse,
|
|
322
|
+
typedStruct
|
|
323
|
+
} from 'is-kit';
|
|
316
324
|
|
|
317
325
|
type PostResponse = ApiResponse<'/posts/{id}', 'get'>;
|
|
318
326
|
|
package/dist/index.d.mts
CHANGED
|
@@ -31,20 +31,26 @@ type SchemaField = Predicate<unknown> | OptionalSchemaField<Predicate<unknown>>;
|
|
|
31
31
|
* Object schema consumed by `struct`.
|
|
32
32
|
*/
|
|
33
33
|
type Schema = Readonly<Record<string, SchemaField>>;
|
|
34
|
-
|
|
34
|
+
/**
|
|
35
|
+
* Object schema shape preserving concrete keys.
|
|
36
|
+
*/
|
|
37
|
+
type SchemaShape<S extends object> = Readonly<{
|
|
38
|
+
[K in keyof S]: SchemaField;
|
|
39
|
+
}>;
|
|
40
|
+
type Simplify<T> = {
|
|
35
41
|
[K in keyof T]: T[K];
|
|
36
42
|
};
|
|
37
43
|
type InferSchemaField<F> = F extends Predicate<unknown> ? GuardedOf<F> : F extends OptionalSchemaField<infer G> ? GuardedOf<G> : never;
|
|
38
|
-
type RequiredSchemaKeys<S extends
|
|
44
|
+
type RequiredSchemaKeys<S extends SchemaShape<S>> = {
|
|
39
45
|
[K in keyof S]-?: S[K] extends OptionalSchemaField<Predicate<unknown>> ? never : K;
|
|
40
46
|
}[keyof S];
|
|
41
|
-
type OptionalSchemaKeys<S extends
|
|
47
|
+
type OptionalSchemaKeys<S extends SchemaShape<S>> = {
|
|
42
48
|
[K in keyof S]-?: S[K] extends OptionalSchemaField<Predicate<unknown>> ? K : never;
|
|
43
49
|
}[keyof S];
|
|
44
50
|
/**
|
|
45
51
|
* Infers the readonly object type produced by a `struct` schema.
|
|
46
52
|
*/
|
|
47
|
-
type InferSchema<S extends
|
|
53
|
+
type InferSchema<S extends SchemaShape<S>> = Simplify<{
|
|
48
54
|
readonly [K in RequiredSchemaKeys<S>]: InferSchemaField<S[K]>;
|
|
49
55
|
} & {
|
|
50
56
|
readonly [K in OptionalSchemaKeys<S>]?: InferSchemaField<S[K]>;
|
|
@@ -546,17 +552,14 @@ declare function optionalKey<G extends Predicate<unknown>>(guard: G): OptionalSc
|
|
|
546
552
|
* rejects extra keys when `exact: true`.
|
|
547
553
|
*
|
|
548
554
|
* @param schema Record of property guards.
|
|
549
|
-
* @param options When `{ exact: true }`, disallows
|
|
555
|
+
* @param options When `{ exact: true }`, disallows own enumerable string-key
|
|
556
|
+
* properties not in `schema`.
|
|
550
557
|
* @returns Predicate that narrows to the inferred struct type.
|
|
551
558
|
*/
|
|
552
|
-
declare function struct<S extends
|
|
559
|
+
declare function struct<const S extends SchemaShape<S>>(schema: S, options?: {
|
|
553
560
|
exact?: boolean;
|
|
554
561
|
}): Predicate<InferSchema<S>>;
|
|
555
562
|
|
|
556
|
-
type Simplify<T> = {
|
|
557
|
-
[K in keyof T]: T[K];
|
|
558
|
-
};
|
|
559
|
-
type StringKeyOf<T> = Extract<keyof T, string>;
|
|
560
563
|
type OptionalKeys<T> = {
|
|
561
564
|
[K in keyof T]-?: {} extends Pick<T, K> ? K : never;
|
|
562
565
|
}[keyof T];
|
|
@@ -569,7 +572,7 @@ type TypedStructShape<T extends object> = {
|
|
|
569
572
|
type NoExtraKeys<S, Shape> = S & {
|
|
570
573
|
readonly [K in Exclude<keyof S, keyof Shape>]: never;
|
|
571
574
|
};
|
|
572
|
-
type
|
|
575
|
+
type TypedStructFields<T extends object, S extends TypedStructShape<T>> = NoExtraKeys<S, TypedStructShape<T>>;
|
|
573
576
|
/**
|
|
574
577
|
* Creates a `struct` builder checked against an existing object type.
|
|
575
578
|
* Optional target keys must also be declared with `optionalKey` so type drift
|
|
@@ -577,9 +580,9 @@ type TypedStructTarget<T extends object> = Simplify<Readonly<Pick<T, StringKeyOf
|
|
|
577
580
|
*
|
|
578
581
|
* @returns Builder that rejects missing, extra, or incompatible struct fields.
|
|
579
582
|
*/
|
|
580
|
-
declare function typedStruct<T extends object>(): <const S extends TypedStructShape<T>>(fields:
|
|
583
|
+
declare function typedStruct<T extends object>(): <const S extends TypedStructShape<T>>(fields: TypedStructFields<T, S>, options?: {
|
|
581
584
|
exact?: boolean;
|
|
582
|
-
}) => Predicate<
|
|
585
|
+
}) => Predicate<InferSchema<TypedStructFields<T, S>>>;
|
|
583
586
|
|
|
584
587
|
/**
|
|
585
588
|
* Validates a fixed-length tuple by applying element-wise guards.
|
package/dist/index.d.ts
CHANGED
|
@@ -31,20 +31,26 @@ type SchemaField = Predicate<unknown> | OptionalSchemaField<Predicate<unknown>>;
|
|
|
31
31
|
* Object schema consumed by `struct`.
|
|
32
32
|
*/
|
|
33
33
|
type Schema = Readonly<Record<string, SchemaField>>;
|
|
34
|
-
|
|
34
|
+
/**
|
|
35
|
+
* Object schema shape preserving concrete keys.
|
|
36
|
+
*/
|
|
37
|
+
type SchemaShape<S extends object> = Readonly<{
|
|
38
|
+
[K in keyof S]: SchemaField;
|
|
39
|
+
}>;
|
|
40
|
+
type Simplify<T> = {
|
|
35
41
|
[K in keyof T]: T[K];
|
|
36
42
|
};
|
|
37
43
|
type InferSchemaField<F> = F extends Predicate<unknown> ? GuardedOf<F> : F extends OptionalSchemaField<infer G> ? GuardedOf<G> : never;
|
|
38
|
-
type RequiredSchemaKeys<S extends
|
|
44
|
+
type RequiredSchemaKeys<S extends SchemaShape<S>> = {
|
|
39
45
|
[K in keyof S]-?: S[K] extends OptionalSchemaField<Predicate<unknown>> ? never : K;
|
|
40
46
|
}[keyof S];
|
|
41
|
-
type OptionalSchemaKeys<S extends
|
|
47
|
+
type OptionalSchemaKeys<S extends SchemaShape<S>> = {
|
|
42
48
|
[K in keyof S]-?: S[K] extends OptionalSchemaField<Predicate<unknown>> ? K : never;
|
|
43
49
|
}[keyof S];
|
|
44
50
|
/**
|
|
45
51
|
* Infers the readonly object type produced by a `struct` schema.
|
|
46
52
|
*/
|
|
47
|
-
type InferSchema<S extends
|
|
53
|
+
type InferSchema<S extends SchemaShape<S>> = Simplify<{
|
|
48
54
|
readonly [K in RequiredSchemaKeys<S>]: InferSchemaField<S[K]>;
|
|
49
55
|
} & {
|
|
50
56
|
readonly [K in OptionalSchemaKeys<S>]?: InferSchemaField<S[K]>;
|
|
@@ -546,17 +552,14 @@ declare function optionalKey<G extends Predicate<unknown>>(guard: G): OptionalSc
|
|
|
546
552
|
* rejects extra keys when `exact: true`.
|
|
547
553
|
*
|
|
548
554
|
* @param schema Record of property guards.
|
|
549
|
-
* @param options When `{ exact: true }`, disallows
|
|
555
|
+
* @param options When `{ exact: true }`, disallows own enumerable string-key
|
|
556
|
+
* properties not in `schema`.
|
|
550
557
|
* @returns Predicate that narrows to the inferred struct type.
|
|
551
558
|
*/
|
|
552
|
-
declare function struct<S extends
|
|
559
|
+
declare function struct<const S extends SchemaShape<S>>(schema: S, options?: {
|
|
553
560
|
exact?: boolean;
|
|
554
561
|
}): Predicate<InferSchema<S>>;
|
|
555
562
|
|
|
556
|
-
type Simplify<T> = {
|
|
557
|
-
[K in keyof T]: T[K];
|
|
558
|
-
};
|
|
559
|
-
type StringKeyOf<T> = Extract<keyof T, string>;
|
|
560
563
|
type OptionalKeys<T> = {
|
|
561
564
|
[K in keyof T]-?: {} extends Pick<T, K> ? K : never;
|
|
562
565
|
}[keyof T];
|
|
@@ -569,7 +572,7 @@ type TypedStructShape<T extends object> = {
|
|
|
569
572
|
type NoExtraKeys<S, Shape> = S & {
|
|
570
573
|
readonly [K in Exclude<keyof S, keyof Shape>]: never;
|
|
571
574
|
};
|
|
572
|
-
type
|
|
575
|
+
type TypedStructFields<T extends object, S extends TypedStructShape<T>> = NoExtraKeys<S, TypedStructShape<T>>;
|
|
573
576
|
/**
|
|
574
577
|
* Creates a `struct` builder checked against an existing object type.
|
|
575
578
|
* Optional target keys must also be declared with `optionalKey` so type drift
|
|
@@ -577,9 +580,9 @@ type TypedStructTarget<T extends object> = Simplify<Readonly<Pick<T, StringKeyOf
|
|
|
577
580
|
*
|
|
578
581
|
* @returns Builder that rejects missing, extra, or incompatible struct fields.
|
|
579
582
|
*/
|
|
580
|
-
declare function typedStruct<T extends object>(): <const S extends TypedStructShape<T>>(fields:
|
|
583
|
+
declare function typedStruct<T extends object>(): <const S extends TypedStructShape<T>>(fields: TypedStructFields<T, S>, options?: {
|
|
581
584
|
exact?: boolean;
|
|
582
|
-
}) => Predicate<
|
|
585
|
+
}) => Predicate<InferSchema<TypedStructFields<T, S>>>;
|
|
583
586
|
|
|
584
587
|
/**
|
|
585
588
|
* Validates a fixed-length tuple by applying element-wise guards.
|
package/dist/index.js
CHANGED
|
@@ -459,7 +459,9 @@ function struct(schema, options) {
|
|
|
459
459
|
const requiredEntries = [];
|
|
460
460
|
const optionalEntries = [];
|
|
461
461
|
const schemaKeys = [];
|
|
462
|
-
for (const
|
|
462
|
+
for (const key in schema) {
|
|
463
|
+
if (!Object.hasOwn(schema, key)) continue;
|
|
464
|
+
const field = schema[key];
|
|
463
465
|
schemaKeys.push(key);
|
|
464
466
|
if (isOptionalSchemaField(field)) {
|
|
465
467
|
optionalEntries.push([key, field.guard]);
|
package/dist/index.mjs
CHANGED
|
@@ -359,7 +359,9 @@ function struct(schema, options) {
|
|
|
359
359
|
const requiredEntries = [];
|
|
360
360
|
const optionalEntries = [];
|
|
361
361
|
const schemaKeys = [];
|
|
362
|
-
for (const
|
|
362
|
+
for (const key in schema) {
|
|
363
|
+
if (!Object.hasOwn(schema, key)) continue;
|
|
364
|
+
const field = schema[key];
|
|
363
365
|
schemaKeys.push(key);
|
|
364
366
|
if (isOptionalSchemaField(field)) {
|
|
365
367
|
optionalEntries.push([key, field.guard]);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "is-kit",
|
|
3
|
-
"version": "1.8.
|
|
3
|
+
"version": "1.8.2",
|
|
4
4
|
"description": "Make 'isXXX' easier. Let's make your code type safe and more readable!",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -38,20 +38,21 @@
|
|
|
38
38
|
"license": "MIT",
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"@types/jest": "^30.0.0",
|
|
41
|
-
"jest": "^30.
|
|
42
|
-
"lefthook": "^2.
|
|
43
|
-
"oxfmt": "^0.
|
|
44
|
-
"oxlint": "^1.
|
|
45
|
-
"ts-jest": "^29.4.
|
|
41
|
+
"jest": "^30.4.2",
|
|
42
|
+
"lefthook": "^2.1.9",
|
|
43
|
+
"oxfmt": "^0.55.0",
|
|
44
|
+
"oxlint": "^1.70.0",
|
|
45
|
+
"ts-jest": "^29.4.11",
|
|
46
46
|
"tsd": "^0.33.0",
|
|
47
47
|
"tsup": "^8.5.0",
|
|
48
|
-
"typedoc": "^0.28.
|
|
49
|
-
"typescript": "^
|
|
48
|
+
"typedoc": "^0.28.19",
|
|
49
|
+
"typescript": "^6.0.3"
|
|
50
50
|
},
|
|
51
51
|
"tsd": {
|
|
52
52
|
"directory": "tests-d",
|
|
53
53
|
"compilerOptions": {
|
|
54
|
-
"baseUrl": "."
|
|
54
|
+
"baseUrl": ".",
|
|
55
|
+
"ignoreDeprecations": "6.0"
|
|
55
56
|
}
|
|
56
57
|
},
|
|
57
58
|
"scripts": {
|