Skip to main content

Import

import { ResponseValidator } from "bytekit/response-validator";

What it does

ResponseValidator validates runtime data against a lightweight schema shape. Use it when you want built-in validation without depending on Zod, Valibot, or another schema library. It checks types, required fields, string/number constraints, patterns, enums, and custom predicates.

Static method

ResponseValidator.validate(data, schema, path?)

const errors = ResponseValidator.validate(data, schema);
ParameterTypeDescription
dataunknownThe value to validate.
schemaValidationSchemaSchema definition describing the expected shape.
pathstring(Optional) Dot-path prefix for error messages. Defaults to "root".
Returns: ValidationError[] — an array of errors. Empty means valid.

ValidationError

PropertyTypeDescription
pathstringDot-path to the invalid field (e.g. root.email).
messagestringHuman-readable error description.
valueunknownThe actual value that failed validation.

ValidationSchema

PropertyTypeDescription
type"string" | "number" | "boolean" | "object" | "array"Expected type.
requiredbooleanIf true, rejects null and undefined.
propertiesRecord<string, ValidationSchema>Nested schemas for object properties.
itemsValidationSchemaSchema applied to each element of an array.
minLengthnumberMinimum string length.
maxLengthnumberMaximum string length.
minimumnumberMinimum number value.
maximumnumberMaximum number value.
patternRegExp | stringRegex pattern the string must match.
enumunknown[]Allowed values.
custom(value: unknown) => boolean | stringCustom validator. Return true to pass, or a string error message to fail.

Examples

Basic object validation

import { ResponseValidator } from "bytekit/response-validator";

const schema = {
  type: "object" as const,
  properties: {
    id: { type: "number" as const, required: true },
    email: {
      type: "string" as const,
      required: true,
      pattern: /.+@.+\..+/,
    },
    role: {
      type: "string" as const,
      enum: ["admin", "user", "guest"],
    },
  },
};

const errors = ResponseValidator.validate(
  { id: 1, email: "ada@example.com", role: "admin" },
  schema,
);

if (errors.length === 0) {
  console.log("Valid!");
} else {
  console.error("Validation failed:", errors);
}

Array with item schema

const listSchema = {
  type: "array" as const,
  items: {
    type: "object" as const,
    properties: {
      id: { type: "number" as const, required: true },
      name: { type: "string" as const, required: true, minLength: 1 },
    },
  },
};

const errors = ResponseValidator.validate(
  [{ id: 1, name: "Alice" }, { id: 2, name: "" }],
  listSchema,
);
// errors[0].path === "root[1].name"

Custom validator

const schema = {
  type: "number" as const,
  required: true,
  custom: (value: unknown) =>
    (value as number) % 2 === 0 || "Must be an even number",
};

const errors = ResponseValidator.validate(3, schema);
// errors[0].message === "Must be an even number"

Using with ApiClient

You can pass a ValidationSchema directly to the validateResponse option on any ApiClient request:
import { ApiClient } from "bytekit/api-client";

const api = new ApiClient({ baseUrl: "https://api.example.com" });

const userSchema = {
  type: "object" as const,
  properties: {
    id: { type: "number" as const, required: true },
    name: { type: "string" as const, required: true },
  },
};

const user = await api.get("/users/1", {
  validateResponse: userSchema,
});
For more expressive schemas, consider using Zod or Valibot adapters instead. ResponseValidator is ideal when you want zero external dependencies.

When to use it

  • You want a package-native validator with no extra dependencies
  • You only need structural and constraint checks at runtime
  • You want to keep your bundle lean