types-json
Type checking for JSON values.
This package uses zod to type check JSON values.
It includes type guards for each of the JSON types, as well as parse functions and corresponding types.
Installation
npm install types-json
yarn add types-json
pnpm add types-json
bun add types-json
Contents
Usage
JSONValue
When using isJSONValue, values which cannot be parsed or serialized properly using JSON.parse and JSON.stringify return false.
Similarly, when using parseJSONValue, invalid values return undefined.
Finally, a zod schema provided representing the JSONValue type.
import { isJSONValue, parseJSONValue, jsonValueSchema } from "types-json";
isJSONValue(undefined);
isJSONValue(null);
isJSONValue(NaN);
isJSONValue(Infinity);
isJSONValue([1, 2, 3]);
isJSONValue([1, 2, () => 3]);
Optional types are also provided:
import { isOptionalJSONValue, parseOptionalJSONValue, optionalJSONValueSchema } from "types-json";
isOptionalJSONValue(undefined);
isOptionalJSONValue({ a: 1, b: undefined });
JSONObject
JSONObject is a type guard and parse function for objects which can be parsed and serialized using JSON.parse and JSON.stringify.
import {
isJSONObject,
parseJSONObject,
jsonObjectSchema
} from "types-json";
isJSONObject({ foo: "bar" });
isJSONObject({ foo: () => "bar" });
parseJSONObject({ foo: "bar" });
parseJSONObject({ foo: () => "bar" });
Optional types are also provided. OptionalJSONObject includes undefined, while NestedOptionalJSONObject only allows for undefined values nested within the object:
import {
isOptionalJSONObject,
parseOptionalJSONObject,
optionalJSONObjectSchema,
isNestedOptionalJSONObject,
parseNestedOptionalJSONObject,
nestedOptionalJSONObjectSchema
} from "types-json";
isOptionalJSONObject(undefined);
isOptionalJSONObject({ a: 1, b: undefined });
isNestedOptionalJSONObject(undefined);
isNestedOptionalJSONObject({ a: 1, b: undefined });
JSONArray
import {
isJSONArray,
parseJSONArray,
jsonArraySchema,
} from "types-json";
isJSONArray([1]);
isJSONArray([1, () => 2]);
parseJSONArray([1]);
parseJSONArray([1, () => 2]);
Optional types are also provided. OptionalJSONArray includes undefined, while NestedOptionalJSONArray only allows for undefined values nested within objects inside the array. Note that undefined values nested within arrays are not allowed, as they are not valid JSON:
import {
isOptionalJSONArray,
parseOptionalJSONArray,
optionalJSONArraySchema,
isNestedOptionalJSONArray,
parseNestedOptionalJSONArray,
nestedOptionalJSONArraySchema
} from "types-json";
isOptionalJSONArray(undefined);
isOptionalJSONArray([1, undefined]);
isOptionalJSONArray([1, { a: 1, b: undefined }]);
isNestedOptionalJSONArray(undefined);
isNestedOptionalJSONArray([1, undefined]);
isNestedOptionalJSONArray([1, { a: 1, b: undefined }]);
JSONPrimitive
import {
isJSONPrimitive,
parseJSONPrimitive,
jsonPrimitiveSchema
} from "types-json";
isJSONPrimitive("foo");
isJSONPrimitive(1);
isJSONPrimitive(true);
isJSONPrimitive(null);
isJSONPrimitive(undefined);
isJSONPrimitive({});
Optional types are also provided:
import {
isOptionalJSONPrimitive,
optionalJSONPrimitiveSchema
} from "types-json";
isOptionalJSONPrimitive("foo");
isOptionalJSONPrimitive(1);
isOptionalJSONPrimitive(true);
isOptionalJSONPrimitive(null);
isOptionalJSONPrimitive(undefined);
isOptionalJSONPrimitive({});
JSONOrderable
JSONOrderable is a type for values which can be reasonably compared using operators such as >, <, >=, and <=:
import {
isJSONOrderable,
parseJSONOrderable,
jsonOrderableSchema
} from "types-json";
isJSONOrderable("foo");
isJSONOrderable(1);
isJSONOrderable(true);
isJSONOrderable(null);
isJSONOrderable(undefined);
isJSONOrderable({});
isJSONOrderable([]);
Optional types are also provided:
import {
isOptionalJSONOrderable,
optionalJSONOrderableSchema
} from "types-json";
isOptionalJSONOrderable("foo");
isOptionalJSONOrderable(1);
isOptionalJSONOrderable(true);
isOptionalJSONOrderable(null);
isOptionalJSONOrderable(undefined);
isOptionalJSONOrderable({});
isOptionalJSONOrderable([]);
String
import {
isString,
parseString,
stringSchema
} from "types-json";
isString("foo");
isString(1);
parseString("foo");
parseString(1);
Optional types are also provided:
import {
isOptionalString,
optionalStringSchema
} from "types-json";
isOptionalString("foo");
isOptionalString(undefined);
Number
import {
isNumber,
parseNumber,
numberSchema
} from "types-json";
isNumber(1);
isNumber("1");
parseNumber(1);
parseNumber("1");
Optional types are also provided:
import {
isOptionalNumber,
optionalNumberSchema
} from "types-json";
isOptionalNumber(1);
isOptionalNumber(undefined);
Boolean
import {
isBoolean,
parseBoolean,
booleanSchema
} from "types-json";
isBoolean(true);
isBoolean("true");
parseBoolean(true);
parseBoolean("true");
Optional types are also provided:
import {
isOptionalBoolean,
optionalBooleanSchema
} from "types-json";
isOptionalBoolean(true);
isOptionalBoolean(undefined);
Null
import {
isNull,
parseNull,
nullSchema
};
isNull(null);
isNull("not null");
parseNull(null);
parseNull("not null");
Optional types are also provided:
import {
isOptionalNull,
optionalNullSchema
} from "types-json";
isOptionalNull(null);
isOptionalNull(undefined);
Undefined
Finally, an isUndefined type guard is provided:
import { isUndefined } from "types-json";
isUndefined(undefined);
isUndefined("string");
Related Packages
Dependencies
- is-zod: Typeguard to check if a value matches a zod schema
- zod: TypeScript-first schema declaration and validation library with static type inference
License 
MIT - MIT License