is-kit 1.11.1 → 1.11.3
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/dist/index.d.mts +41 -20
- package/dist/index.d.ts +41 -20
- package/dist/index.js +3 -1
- package/dist/index.mjs +3 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -39,6 +39,13 @@ type SchemaField = Predicate<unknown> | OptionalSchemaField<Predicate<unknown>>;
|
|
|
39
39
|
* Object schema consumed by `struct`.
|
|
40
40
|
*/
|
|
41
41
|
type Schema = Readonly<Record<string, SchemaField>>;
|
|
42
|
+
/**
|
|
43
|
+
* Configures object-key validation for `struct` and `typedStruct`.
|
|
44
|
+
*/
|
|
45
|
+
type StructOptions = Readonly<{
|
|
46
|
+
/** Rejects extra own enumerable string-key properties when enabled. */
|
|
47
|
+
exact?: boolean;
|
|
48
|
+
}>;
|
|
42
49
|
/**
|
|
43
50
|
* Object schema shape preserving concrete keys.
|
|
44
51
|
*/
|
|
@@ -63,6 +70,34 @@ type InferSchema<S extends SchemaShape<S>> = Simplify<{
|
|
|
63
70
|
} & {
|
|
64
71
|
readonly [K in OptionalSchemaKeys<S>]?: InferSchemaField<S[K]>;
|
|
65
72
|
}>;
|
|
73
|
+
/**
|
|
74
|
+
* Extracts optional keys from an object type.
|
|
75
|
+
*/
|
|
76
|
+
type OptionalObjectKeys<T> = {
|
|
77
|
+
[K in keyof T]-?: {} extends Pick<T, K> ? K : never;
|
|
78
|
+
}[keyof T];
|
|
79
|
+
/**
|
|
80
|
+
* Extracts required keys from an object type.
|
|
81
|
+
*/
|
|
82
|
+
type RequiredObjectKeys<T> = Exclude<keyof T, OptionalObjectKeys<T>>;
|
|
83
|
+
/**
|
|
84
|
+
* Schema shape checked against an existing object type by `typedStruct`.
|
|
85
|
+
*/
|
|
86
|
+
type TypedStructShape<T extends object> = {
|
|
87
|
+
readonly [K in Extract<RequiredObjectKeys<T>, string>]-?: Predicate<T[K]>;
|
|
88
|
+
} & {
|
|
89
|
+
readonly [K in Extract<OptionalObjectKeys<T>, string>]-?: OptionalSchemaField<Predicate<T[K]>>;
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* Keeps a schema type while rejecting keys outside the expected shape.
|
|
93
|
+
*/
|
|
94
|
+
type NoExtraKeys<S, Shape> = S & {
|
|
95
|
+
readonly [K in Exclude<keyof S, keyof Shape>]: never;
|
|
96
|
+
};
|
|
97
|
+
/**
|
|
98
|
+
* Field set accepted by `typedStruct` for a target object type.
|
|
99
|
+
*/
|
|
100
|
+
type TypedStructFields<T extends object, S extends TypedStructShape<T>> = NoExtraKeys<S, TypedStructShape<T>>;
|
|
66
101
|
|
|
67
102
|
/**
|
|
68
103
|
* Asserts that a value satisfies a guard or refinement, otherwise throws.
|
|
@@ -658,33 +693,19 @@ declare function optionalKey<G extends Predicate<unknown>>(guard: G): OptionalSc
|
|
|
658
693
|
* properties not in `schema`.
|
|
659
694
|
* @returns Predicate that narrows to the inferred struct type.
|
|
660
695
|
*/
|
|
661
|
-
declare function struct<const S extends SchemaShape<S>>(schema: S, options?:
|
|
662
|
-
exact?: boolean;
|
|
663
|
-
}): Predicate<InferSchema<S>>;
|
|
696
|
+
declare function struct<const S extends SchemaShape<S>>(schema: S, options?: StructOptions): Predicate<InferSchema<S>>;
|
|
664
697
|
|
|
665
|
-
type OptionalKeys<T> = {
|
|
666
|
-
[K in keyof T]-?: {} extends Pick<T, K> ? K : never;
|
|
667
|
-
}[keyof T];
|
|
668
|
-
type RequiredKeys<T> = Exclude<keyof T, OptionalKeys<T>>;
|
|
669
|
-
type TypedStructShape<T extends object> = {
|
|
670
|
-
readonly [K in Extract<RequiredKeys<T>, string>]-?: Predicate<T[K]>;
|
|
671
|
-
} & {
|
|
672
|
-
readonly [K in Extract<OptionalKeys<T>, string>]-?: OptionalSchemaField<Predicate<T[K]>>;
|
|
673
|
-
};
|
|
674
|
-
type NoExtraKeys<S, Shape> = S & {
|
|
675
|
-
readonly [K in Exclude<keyof S, keyof Shape>]: never;
|
|
676
|
-
};
|
|
677
|
-
type TypedStructFields<T extends object, S extends TypedStructShape<T>> = NoExtraKeys<S, TypedStructShape<T>>;
|
|
678
698
|
/**
|
|
679
699
|
* Creates a `struct` builder checked against an existing object type.
|
|
680
700
|
* Optional target keys must also be declared with `optionalKey` so type drift
|
|
681
701
|
* stays visible when the target type changes.
|
|
682
702
|
*
|
|
703
|
+
* Target-compatible field guards may narrow further than the target field type;
|
|
704
|
+
* the returned predicate preserves those narrower inferred types.
|
|
705
|
+
*
|
|
683
706
|
* @returns Builder that rejects missing, extra, or incompatible struct fields.
|
|
684
707
|
*/
|
|
685
|
-
declare function typedStruct<T extends object>(): <const S extends TypedStructShape<T>>(fields: TypedStructFields<T, S>, options?:
|
|
686
|
-
exact?: boolean;
|
|
687
|
-
}) => Predicate<InferSchema<TypedStructFields<T, S>>>;
|
|
708
|
+
declare function typedStruct<T extends object>(): <const S extends TypedStructShape<T>>(fields: TypedStructFields<T, S>, options?: StructOptions) => Predicate<InferSchema<TypedStructFields<T, S>>>;
|
|
688
709
|
|
|
689
710
|
/**
|
|
690
711
|
* Validates a fixed-length tuple by applying element-wise guards.
|
|
@@ -748,4 +769,4 @@ declare const everyOwnEnumerableEntry: (values: Record<string, unknown>, keyPred
|
|
|
748
769
|
*/
|
|
749
770
|
declare const toBooleanPredicates: <A>(predicates: readonly ((value: A) => boolean)[]) => ReadonlyArray<(value: A) => boolean>;
|
|
750
771
|
|
|
751
|
-
export { type ChainResult, type Guard, type GuardedOf, type GuardedWithin, type InferSchema, type OptionalSchemaField, type OutOfGuards, type ParseResult, type Predicate, type Primitive, type Refine, type RefineChain, type Refinement, type Schema, type SchemaField, and, andAll, arrayOf, assert, define, equals, equalsBy, equalsKey, everyArrayValue, everyMapEntry, everyOwnEnumerableEntry, everySetValue, everyTupleValue, guardIn, hasKey, hasKeys, isArray, isArrayBuffer, isAsyncIterable, isBigInt, isBlob, isBoolean, isDataView, isDate, isError, isFile, isFiniteNumber, isFunction, isInfiniteNumber, isInstanceOf, isInteger, isIterable, isMap, isNaN, isNegative, isNil, isNull, isNumber, isNumberPrimitive, isObject, isPlainObject, isPositive, isPrimitive, isPromiseLike, isRegExp, isSafeInteger, isSet, isString, isSymbol, isTypedArray, isURL, isUndefined, isWeakMap, isWeakSet, isZero, lazy, mapOf, narrowKeyTo, nonEmptyArrayOf, nonNull, not, nullable, nullish, oneOf, oneOfValues, optional, optionalKey, or, predicateToRefine, recordOf, required, safeJsonParse, safeParse, safeParseWith, setOf, struct, toBooleanPredicates, tupleOf, typedStruct };
|
|
772
|
+
export { type ChainResult, type Guard, type GuardedOf, type GuardedWithin, type InferSchema, type NoExtraKeys, type OptionalObjectKeys, type OptionalSchemaField, type OutOfGuards, type ParseResult, type Predicate, type Primitive, type Refine, type RefineChain, type Refinement, type RequiredObjectKeys, type Schema, type SchemaField, type StructOptions, type TypedStructFields, type TypedStructShape, and, andAll, arrayOf, assert, define, equals, equalsBy, equalsKey, everyArrayValue, everyMapEntry, everyOwnEnumerableEntry, everySetValue, everyTupleValue, guardIn, hasKey, hasKeys, isArray, isArrayBuffer, isAsyncIterable, isBigInt, isBlob, isBoolean, isDataView, isDate, isError, isFile, isFiniteNumber, isFunction, isInfiniteNumber, isInstanceOf, isInteger, isIterable, isMap, isNaN, isNegative, isNil, isNull, isNumber, isNumberPrimitive, isObject, isPlainObject, isPositive, isPrimitive, isPromiseLike, isRegExp, isSafeInteger, isSet, isString, isSymbol, isTypedArray, isURL, isUndefined, isWeakMap, isWeakSet, isZero, lazy, mapOf, narrowKeyTo, nonEmptyArrayOf, nonNull, not, nullable, nullish, oneOf, oneOfValues, optional, optionalKey, or, predicateToRefine, recordOf, required, safeJsonParse, safeParse, safeParseWith, setOf, struct, toBooleanPredicates, tupleOf, typedStruct };
|
package/dist/index.d.ts
CHANGED
|
@@ -39,6 +39,13 @@ type SchemaField = Predicate<unknown> | OptionalSchemaField<Predicate<unknown>>;
|
|
|
39
39
|
* Object schema consumed by `struct`.
|
|
40
40
|
*/
|
|
41
41
|
type Schema = Readonly<Record<string, SchemaField>>;
|
|
42
|
+
/**
|
|
43
|
+
* Configures object-key validation for `struct` and `typedStruct`.
|
|
44
|
+
*/
|
|
45
|
+
type StructOptions = Readonly<{
|
|
46
|
+
/** Rejects extra own enumerable string-key properties when enabled. */
|
|
47
|
+
exact?: boolean;
|
|
48
|
+
}>;
|
|
42
49
|
/**
|
|
43
50
|
* Object schema shape preserving concrete keys.
|
|
44
51
|
*/
|
|
@@ -63,6 +70,34 @@ type InferSchema<S extends SchemaShape<S>> = Simplify<{
|
|
|
63
70
|
} & {
|
|
64
71
|
readonly [K in OptionalSchemaKeys<S>]?: InferSchemaField<S[K]>;
|
|
65
72
|
}>;
|
|
73
|
+
/**
|
|
74
|
+
* Extracts optional keys from an object type.
|
|
75
|
+
*/
|
|
76
|
+
type OptionalObjectKeys<T> = {
|
|
77
|
+
[K in keyof T]-?: {} extends Pick<T, K> ? K : never;
|
|
78
|
+
}[keyof T];
|
|
79
|
+
/**
|
|
80
|
+
* Extracts required keys from an object type.
|
|
81
|
+
*/
|
|
82
|
+
type RequiredObjectKeys<T> = Exclude<keyof T, OptionalObjectKeys<T>>;
|
|
83
|
+
/**
|
|
84
|
+
* Schema shape checked against an existing object type by `typedStruct`.
|
|
85
|
+
*/
|
|
86
|
+
type TypedStructShape<T extends object> = {
|
|
87
|
+
readonly [K in Extract<RequiredObjectKeys<T>, string>]-?: Predicate<T[K]>;
|
|
88
|
+
} & {
|
|
89
|
+
readonly [K in Extract<OptionalObjectKeys<T>, string>]-?: OptionalSchemaField<Predicate<T[K]>>;
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* Keeps a schema type while rejecting keys outside the expected shape.
|
|
93
|
+
*/
|
|
94
|
+
type NoExtraKeys<S, Shape> = S & {
|
|
95
|
+
readonly [K in Exclude<keyof S, keyof Shape>]: never;
|
|
96
|
+
};
|
|
97
|
+
/**
|
|
98
|
+
* Field set accepted by `typedStruct` for a target object type.
|
|
99
|
+
*/
|
|
100
|
+
type TypedStructFields<T extends object, S extends TypedStructShape<T>> = NoExtraKeys<S, TypedStructShape<T>>;
|
|
66
101
|
|
|
67
102
|
/**
|
|
68
103
|
* Asserts that a value satisfies a guard or refinement, otherwise throws.
|
|
@@ -658,33 +693,19 @@ declare function optionalKey<G extends Predicate<unknown>>(guard: G): OptionalSc
|
|
|
658
693
|
* properties not in `schema`.
|
|
659
694
|
* @returns Predicate that narrows to the inferred struct type.
|
|
660
695
|
*/
|
|
661
|
-
declare function struct<const S extends SchemaShape<S>>(schema: S, options?:
|
|
662
|
-
exact?: boolean;
|
|
663
|
-
}): Predicate<InferSchema<S>>;
|
|
696
|
+
declare function struct<const S extends SchemaShape<S>>(schema: S, options?: StructOptions): Predicate<InferSchema<S>>;
|
|
664
697
|
|
|
665
|
-
type OptionalKeys<T> = {
|
|
666
|
-
[K in keyof T]-?: {} extends Pick<T, K> ? K : never;
|
|
667
|
-
}[keyof T];
|
|
668
|
-
type RequiredKeys<T> = Exclude<keyof T, OptionalKeys<T>>;
|
|
669
|
-
type TypedStructShape<T extends object> = {
|
|
670
|
-
readonly [K in Extract<RequiredKeys<T>, string>]-?: Predicate<T[K]>;
|
|
671
|
-
} & {
|
|
672
|
-
readonly [K in Extract<OptionalKeys<T>, string>]-?: OptionalSchemaField<Predicate<T[K]>>;
|
|
673
|
-
};
|
|
674
|
-
type NoExtraKeys<S, Shape> = S & {
|
|
675
|
-
readonly [K in Exclude<keyof S, keyof Shape>]: never;
|
|
676
|
-
};
|
|
677
|
-
type TypedStructFields<T extends object, S extends TypedStructShape<T>> = NoExtraKeys<S, TypedStructShape<T>>;
|
|
678
698
|
/**
|
|
679
699
|
* Creates a `struct` builder checked against an existing object type.
|
|
680
700
|
* Optional target keys must also be declared with `optionalKey` so type drift
|
|
681
701
|
* stays visible when the target type changes.
|
|
682
702
|
*
|
|
703
|
+
* Target-compatible field guards may narrow further than the target field type;
|
|
704
|
+
* the returned predicate preserves those narrower inferred types.
|
|
705
|
+
*
|
|
683
706
|
* @returns Builder that rejects missing, extra, or incompatible struct fields.
|
|
684
707
|
*/
|
|
685
|
-
declare function typedStruct<T extends object>(): <const S extends TypedStructShape<T>>(fields: TypedStructFields<T, S>, options?:
|
|
686
|
-
exact?: boolean;
|
|
687
|
-
}) => Predicate<InferSchema<TypedStructFields<T, S>>>;
|
|
708
|
+
declare function typedStruct<T extends object>(): <const S extends TypedStructShape<T>>(fields: TypedStructFields<T, S>, options?: StructOptions) => Predicate<InferSchema<TypedStructFields<T, S>>>;
|
|
688
709
|
|
|
689
710
|
/**
|
|
690
711
|
* Validates a fixed-length tuple by applying element-wise guards.
|
|
@@ -748,4 +769,4 @@ declare const everyOwnEnumerableEntry: (values: Record<string, unknown>, keyPred
|
|
|
748
769
|
*/
|
|
749
770
|
declare const toBooleanPredicates: <A>(predicates: readonly ((value: A) => boolean)[]) => ReadonlyArray<(value: A) => boolean>;
|
|
750
771
|
|
|
751
|
-
export { type ChainResult, type Guard, type GuardedOf, type GuardedWithin, type InferSchema, type OptionalSchemaField, type OutOfGuards, type ParseResult, type Predicate, type Primitive, type Refine, type RefineChain, type Refinement, type Schema, type SchemaField, and, andAll, arrayOf, assert, define, equals, equalsBy, equalsKey, everyArrayValue, everyMapEntry, everyOwnEnumerableEntry, everySetValue, everyTupleValue, guardIn, hasKey, hasKeys, isArray, isArrayBuffer, isAsyncIterable, isBigInt, isBlob, isBoolean, isDataView, isDate, isError, isFile, isFiniteNumber, isFunction, isInfiniteNumber, isInstanceOf, isInteger, isIterable, isMap, isNaN, isNegative, isNil, isNull, isNumber, isNumberPrimitive, isObject, isPlainObject, isPositive, isPrimitive, isPromiseLike, isRegExp, isSafeInteger, isSet, isString, isSymbol, isTypedArray, isURL, isUndefined, isWeakMap, isWeakSet, isZero, lazy, mapOf, narrowKeyTo, nonEmptyArrayOf, nonNull, not, nullable, nullish, oneOf, oneOfValues, optional, optionalKey, or, predicateToRefine, recordOf, required, safeJsonParse, safeParse, safeParseWith, setOf, struct, toBooleanPredicates, tupleOf, typedStruct };
|
|
772
|
+
export { type ChainResult, type Guard, type GuardedOf, type GuardedWithin, type InferSchema, type NoExtraKeys, type OptionalObjectKeys, type OptionalSchemaField, type OutOfGuards, type ParseResult, type Predicate, type Primitive, type Refine, type RefineChain, type Refinement, type RequiredObjectKeys, type Schema, type SchemaField, type StructOptions, type TypedStructFields, type TypedStructShape, and, andAll, arrayOf, assert, define, equals, equalsBy, equalsKey, everyArrayValue, everyMapEntry, everyOwnEnumerableEntry, everySetValue, everyTupleValue, guardIn, hasKey, hasKeys, isArray, isArrayBuffer, isAsyncIterable, isBigInt, isBlob, isBoolean, isDataView, isDate, isError, isFile, isFiniteNumber, isFunction, isInfiniteNumber, isInstanceOf, isInteger, isIterable, isMap, isNaN, isNegative, isNil, isNull, isNumber, isNumberPrimitive, isObject, isPlainObject, isPositive, isPrimitive, isPromiseLike, isRegExp, isSafeInteger, isSet, isString, isSymbol, isTypedArray, isURL, isUndefined, isWeakMap, isWeakSet, isZero, lazy, mapOf, narrowKeyTo, nonEmptyArrayOf, nonNull, not, nullable, nullish, oneOf, oneOfValues, optional, optionalKey, or, predicateToRefine, recordOf, required, safeJsonParse, safeParse, safeParseWith, setOf, struct, toBooleanPredicates, tupleOf, typedStruct };
|
package/dist/index.js
CHANGED
|
@@ -473,7 +473,9 @@ var isOptionalSchemaField = define(
|
|
|
473
473
|
var hasRequiredKeys = (obj, entries) => (
|
|
474
474
|
// WHY: Required fields must be own properties; inherited values should not
|
|
475
475
|
// satisfy a schema because `struct` models object payload shape, not prototype chains.
|
|
476
|
-
entries.every(
|
|
476
|
+
entries.every(
|
|
477
|
+
([key, guard]) => hasOwnPropertyKey(obj, key) && guard(obj[key])
|
|
478
|
+
)
|
|
477
479
|
);
|
|
478
480
|
var hasValidOptionalKeys = (obj, entries) => (
|
|
479
481
|
// WHY: Optional keys are validated only when present. Missing keys stay valid
|
package/dist/index.mjs
CHANGED
|
@@ -369,7 +369,9 @@ var isOptionalSchemaField = define(
|
|
|
369
369
|
var hasRequiredKeys = (obj, entries) => (
|
|
370
370
|
// WHY: Required fields must be own properties; inherited values should not
|
|
371
371
|
// satisfy a schema because `struct` models object payload shape, not prototype chains.
|
|
372
|
-
entries.every(
|
|
372
|
+
entries.every(
|
|
373
|
+
([key, guard]) => hasOwnPropertyKey(obj, key) && guard(obj[key])
|
|
374
|
+
)
|
|
373
375
|
);
|
|
374
376
|
var hasValidOptionalKeys = (obj, entries) => (
|
|
375
377
|
// WHY: Optional keys are validated only when present. Missing keys stay valid
|