is-kit 1.8.2 → 1.9.0

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 CHANGED
@@ -264,7 +264,39 @@ assert(isString, input, 'Expected a string');
264
264
  input.toUpperCase();
265
265
  ```
266
266
 
267
- ### 8. Narrow object keys
267
+ ### 8. Decode and validate JSON input
268
+
269
+ Use `safeJsonParse` at a JSON text boundary. It decodes the text, treats the
270
+ result as `unknown`, and only returns the value after the guard accepts it.
271
+ Invalid JSON and guard mismatches both return `{ valid: false }`; values are not
272
+ coerced to satisfy the guard.
273
+
274
+ ```ts
275
+ import { isString, safeJsonParse, typedStruct } from 'is-kit';
276
+
277
+ type User = {
278
+ id: string;
279
+ name: string;
280
+ };
281
+
282
+ const isUser = typedStruct<User>()({
283
+ id: isString,
284
+ name: isString
285
+ });
286
+
287
+ declare const input: string;
288
+ const result = safeJsonParse(input, isUser);
289
+
290
+ if (result.valid) {
291
+ result.value.name.toUpperCase();
292
+ }
293
+ ```
294
+
295
+ `safeJsonParse` is a decode-then-guard helper. It does not perform schema
296
+ coercion and does not depend on a transport or schema format such as HTTP or
297
+ OpenAPI.
298
+
299
+ ### 9. Narrow object keys
268
300
 
269
301
  Use key helpers when the important part of a value is one property.
270
302
 
@@ -378,7 +410,7 @@ The library is organized around a few small building blocks:
378
410
  - **Collections**: `arrayOf`, `nonEmptyArrayOf`, `tupleOf`, `setOf`, `mapOf`, `recordOf`
379
411
  - **Literals**: `oneOfValues`, `equals`, `equalsBy`, `equalsKey`
380
412
  - **Nullish handling**: `nullable`, `nonNull`, `nullish`, `optional`, `required`
381
- - **Result helpers**: `safeParse`, `safeParseWith`, `assert`
413
+ - **Result helpers**: `safeParse`, `safeParseWith`, `safeJsonParse`, `assert`
382
414
 
383
415
  For the full API list and dedicated pages, use the docs site below.
384
416
 
package/dist/index.d.mts CHANGED
@@ -232,6 +232,14 @@ declare function safeParse(fn: (value: unknown) => boolean, value: unknown): Par
232
232
  */
233
233
  declare function safeParseWith<T>(guard: Guard<T>): (value: unknown) => ParseResult<T>;
234
234
  declare function safeParseWith<A, B extends A>(refine: Refine<A, B>): (value: A) => ParseResult<B>;
235
+ /**
236
+ * Decodes JSON text and validates the decoded value without coercion.
237
+ *
238
+ * @param input JSON text to decode.
239
+ * @param guard Guard used to validate the decoded `unknown` value.
240
+ * @returns The decoded value when valid; `{ valid: false }` for invalid JSON or a guard mismatch.
241
+ */
242
+ declare function safeJsonParse<T>(input: string, guard: Predicate<T>): ParseResult<T>;
235
243
 
236
244
  /**
237
245
  * Converts a boolean predicate into a refinement preserving the input type.
@@ -645,4 +653,4 @@ declare const everyOwnEnumerableEntry: (values: Record<string, unknown>, keyPred
645
653
  */
646
654
  declare const toBooleanPredicates: <A>(predicates: readonly ((value: A) => boolean)[]) => ReadonlyArray<(value: A) => boolean>;
647
655
 
648
- 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, isFiniteNumber, isFunction, isInfiniteNumber, isInstanceOf, isInteger, isIterable, isMap, isNaN, isNegative, isNull, isNumber, isNumberPrimitive, isObject, isPlainObject, isPositive, isPrimitive, isPromiseLike, isRegExp, isSafeInteger, isSet, isString, isSymbol, isTypedArray, isURL, isUndefined, isWeakMap, isWeakSet, isZero, mapOf, narrowKeyTo, nonEmptyArrayOf, nonNull, not, nullable, nullish, oneOf, oneOfValues, optional, optionalKey, or, predicateToRefine, recordOf, required, safeParse, safeParseWith, setOf, struct, toBooleanPredicates, tupleOf, typedStruct };
656
+ 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, isFiniteNumber, isFunction, isInfiniteNumber, isInstanceOf, isInteger, isIterable, isMap, isNaN, isNegative, isNull, isNumber, isNumberPrimitive, isObject, isPlainObject, isPositive, isPrimitive, isPromiseLike, isRegExp, isSafeInteger, isSet, isString, isSymbol, isTypedArray, isURL, isUndefined, isWeakMap, isWeakSet, isZero, 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
@@ -232,6 +232,14 @@ declare function safeParse(fn: (value: unknown) => boolean, value: unknown): Par
232
232
  */
233
233
  declare function safeParseWith<T>(guard: Guard<T>): (value: unknown) => ParseResult<T>;
234
234
  declare function safeParseWith<A, B extends A>(refine: Refine<A, B>): (value: A) => ParseResult<B>;
235
+ /**
236
+ * Decodes JSON text and validates the decoded value without coercion.
237
+ *
238
+ * @param input JSON text to decode.
239
+ * @param guard Guard used to validate the decoded `unknown` value.
240
+ * @returns The decoded value when valid; `{ valid: false }` for invalid JSON or a guard mismatch.
241
+ */
242
+ declare function safeJsonParse<T>(input: string, guard: Predicate<T>): ParseResult<T>;
235
243
 
236
244
  /**
237
245
  * Converts a boolean predicate into a refinement preserving the input type.
@@ -645,4 +653,4 @@ declare const everyOwnEnumerableEntry: (values: Record<string, unknown>, keyPred
645
653
  */
646
654
  declare const toBooleanPredicates: <A>(predicates: readonly ((value: A) => boolean)[]) => ReadonlyArray<(value: A) => boolean>;
647
655
 
648
- 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, isFiniteNumber, isFunction, isInfiniteNumber, isInstanceOf, isInteger, isIterable, isMap, isNaN, isNegative, isNull, isNumber, isNumberPrimitive, isObject, isPlainObject, isPositive, isPrimitive, isPromiseLike, isRegExp, isSafeInteger, isSet, isString, isSymbol, isTypedArray, isURL, isUndefined, isWeakMap, isWeakSet, isZero, mapOf, narrowKeyTo, nonEmptyArrayOf, nonNull, not, nullable, nullish, oneOf, oneOfValues, optional, optionalKey, or, predicateToRefine, recordOf, required, safeParse, safeParseWith, setOf, struct, toBooleanPredicates, tupleOf, typedStruct };
656
+ 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, isFiniteNumber, isFunction, isInfiniteNumber, isInstanceOf, isInteger, isIterable, isMap, isNaN, isNegative, isNull, isNumber, isNumberPrimitive, isObject, isPlainObject, isPositive, isPrimitive, isPromiseLike, isRegExp, isSafeInteger, isSet, isString, isSymbol, isTypedArray, isURL, isUndefined, isWeakMap, isWeakSet, isZero, 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
@@ -88,6 +88,7 @@ __export(index_exports, {
88
88
  predicateToRefine: () => predicateToRefine,
89
89
  recordOf: () => recordOf,
90
90
  required: () => required,
91
+ safeJsonParse: () => safeJsonParse,
91
92
  safeParse: () => safeParse,
92
93
  safeParseWith: () => safeParseWith,
93
94
  setOf: () => setOf,
@@ -317,6 +318,15 @@ function safeParse(fn, value) {
317
318
  function safeParseWith(fn) {
318
319
  return (value) => safeParse(fn, value);
319
320
  }
321
+ function safeJsonParse(input, guard) {
322
+ let value;
323
+ try {
324
+ value = JSON.parse(input);
325
+ } catch {
326
+ return { valid: false };
327
+ }
328
+ return safeParse(guard, value);
329
+ }
320
330
 
321
331
  // src/core/predicate.ts
322
332
  var predicateToRefine = (fn) => (value) => fn(value);
@@ -565,6 +575,7 @@ function tupleOf(...guards) {
565
575
  predicateToRefine,
566
576
  recordOf,
567
577
  required,
578
+ safeJsonParse,
568
579
  safeParse,
569
580
  safeParseWith,
570
581
  setOf,
package/dist/index.mjs CHANGED
@@ -217,6 +217,15 @@ function safeParse(fn, value) {
217
217
  function safeParseWith(fn) {
218
218
  return (value) => safeParse(fn, value);
219
219
  }
220
+ function safeJsonParse(input, guard) {
221
+ let value;
222
+ try {
223
+ value = JSON.parse(input);
224
+ } catch {
225
+ return { valid: false };
226
+ }
227
+ return safeParse(guard, value);
228
+ }
220
229
 
221
230
  // src/core/predicate.ts
222
231
  var predicateToRefine = (fn) => (value) => fn(value);
@@ -464,6 +473,7 @@ export {
464
473
  predicateToRefine,
465
474
  recordOf,
466
475
  required,
476
+ safeJsonParse,
467
477
  safeParse,
468
478
  safeParseWith,
469
479
  setOf,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "is-kit",
3
- "version": "1.8.2",
3
+ "version": "1.9.0",
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",