Skip to main content

validate API

Schema validation with WASM-accelerated rule evaluation in web workers.

npm install @rowops/validate

Worker Factory

createValidatorWorker

Creates a web worker for validation.

import { createValidatorWorker } from "@rowops/validate";

const worker = createValidatorWorker();

worker.postMessage({
requestId: "1",
type: "validate",
payload: {
ipcBytes: arrowIpcBuffer,
schema: mySchema,
},
});

worker.onmessage = (event) => {
const response = event.data;
console.log("Valid rows:", response.payload.validCount);
};

Worker Protocol

WorkerRequest

Base request type for validation worker messages.

interface WorkerRequest<T = unknown> {
requestId: string;
type: string;
payload: T;
}

ValidateRequest

Request payload for validation.

interface ValidateRequest {
ipcBytes: Uint8Array;
schema: RowOpsSchema;
options?: {
stopOnFirstError?: boolean;
batchSize?: number;
};
}

ValidateResponse

Response from validation worker.

interface ValidateResponse {
validCount: number;
invalidCount: number;
errors: ValidationError[];
validIpcBytes?: Uint8Array;
invalidIpcBytes?: Uint8Array;
}

interface ValidationError {
rowIndex: number;
field: string;
code: string;
message: string;
value?: unknown;
}

Schema Types

RowOpsSchema

Defines the validation schema structure.

interface RowOpsSchema {
id: string;
name: string;
description?: string;
fields: RowOpsSchemaField[];
versionId?: string;
version?: number;
tier?: "free" | "pro" | "scale" | "enterprise";
}

RowOpsSchemaField

Individual field definition in a schema.

interface RowOpsSchemaField {
key: string;
label: string;
required?: boolean;
type?: "string" | "number" | "date" | "boolean";
regex?: string;
/** Semantic type for built-in pattern validation */
semanticType?: SemanticType;
enumValues?: string[];
transforms?: TransformRule[];
/** Minimum value for number fields */
min?: number;
/** Maximum value for number fields */
max?: number;
/** Minimum length for string fields */
minLength?: number;
/** Maximum length for string fields */
maxLength?: number;
}

type SemanticType =
| "email"
| "phone"
| "url"
| "ssn"
| "zipcode"
| "creditcard"
| "uuid"
| "ipv4"
| "ipv6";

type TransformRule = "trim" | "lowercase" | "uppercase";

Validation Rules

Built-in Validations

RuleDescriptionExample
requiredField must have a non-empty value{ required: true }
typeValue must match type{ type: "number" }
regexValue must match pattern{ regex: "^[A-Z]{2}$" }
semanticTypeValue must match semantic pattern{ semanticType: "email" }
enumValuesValue must be in allowed list{ enumValues: ["A", "B"] }
minNumber must be at least value{ min: 0 }
maxNumber must be at most value{ max: 100 }
minLengthString must have at least N chars{ minLength: 3 }
maxLengthString must have at most N chars{ maxLength: 50 }

Semantic Type Validation

Use built-in patterns for common data types:

const schema: RowOpsSchema = {
id: "users",
name: "Users",
fields: [
{ key: "email", label: "Email", required: true, semanticType: "email" },
{ key: "phone", label: "Phone", semanticType: "phone" },
{ key: "website", label: "Website", semanticType: "url" },
{ key: "ssn", label: "SSN", semanticType: "ssn" },
{ key: "zipcode", label: "ZIP Code", semanticType: "zipcode" },
],
};

Type Validation

const schema: RowOpsSchema = {
id: "contacts",
name: "Contacts",
fields: [
{ key: "email", label: "Email", type: "string", required: true },
{ key: "age", label: "Age", type: "number" },
{ key: "active", label: "Active", type: "boolean" },
{ key: "created", label: "Created", type: "date" },
],
};

Regex Validation

const schema: RowOpsSchema = {
id: "products",
name: "Products",
fields: [
{
key: "sku",
label: "SKU",
required: true,
regex: "^[A-Z]{3}-\\d{4}$", // ABC-1234 format
},
{
key: "email",
label: "Email",
regex: "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$",
},
],
};

Enum Validation

const schema: RowOpsSchema = {
id: "employees",
name: "Employees",
fields: [
{
key: "department",
label: "Department",
required: true,
enumValues: ["Engineering", "Sales", "Marketing", "HR", "Finance"],
},
{
key: "status",
label: "Status",
enumValues: ["active", "inactive", "pending"],
},
],
};

Range Validation

const schema: RowOpsSchema = {
id: "products",
name: "Products",
fields: [
{
key: "price",
label: "Price",
type: "number",
required: true,
min: 0,
max: 10000,
},
{
key: "quantity",
label: "Quantity",
type: "number",
min: 1,
},
{
key: "sku",
label: "SKU",
required: true,
minLength: 5,
maxLength: 20,
},
{
key: "description",
label: "Description",
maxLength: 500,
},
],
};

Error Codes

CodeDescription
requiredRequired field is empty or null
type_numberValue cannot be cast to number
type_booleanValue cannot be cast to boolean
type_dateValue cannot be parsed as date
patternValue doesn't match regex pattern
semanticTypeValue doesn't match semantic type pattern (email, phone, etc.)
enumValue not in allowed enum list
minNumber is below minimum value
maxNumber exceeds maximum value
minLengthString is shorter than minimum length
maxLengthString exceeds maximum length

Usage Example

import { createValidatorWorker } from "@rowops/validate";

async function validateData(arrowIpcBytes: Uint8Array) {
const worker = createValidatorWorker();

return new Promise((resolve, reject) => {
worker.onmessage = (event) => {
const response = event.data;
if (response.type === "error") {
reject(new Error(response.payload.message));
} else {
resolve(response.payload);
}
worker.terminate();
};

worker.onerror = reject;

worker.postMessage({
requestId: crypto.randomUUID(),
type: "validate",
payload: {
ipcBytes: arrowIpcBytes,
schema: {
id: "my-schema",
name: "My Schema",
fields: [
{ key: "name", label: "Name", required: true },
{ key: "email", label: "Email", required: true, regex: "^.+@.+$" },
{ key: "age", label: "Age", type: "number" },
],
},
},
});
});
}

Tier Restrictions

FeatureFreeProScaleEnterprise
Required/type validationYesYesYesYes
Regex validationYesYesYesYes
Enum validationYesYesYesYes
Range validation (min/max)YesYesYesYes
Length validation (minLength/maxLength)YesYesYesYes
Max rows per validation1,000100,0001,000,000Unlimited

See Also