Implementation:Microsoft Playwright Validator Primitives
| Knowledge Sources | |
|---|---|
| Domains | Protocol, Validation |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
Concrete tool for validating protocol message parameters and results against a type schema provided by the Playwright library.
Description
The Validator Primitives module defines the foundational building blocks of Playwright's protocol validation system. It exports a `ValidationError` class, a `Validator` function type, a `ValidatorContext` interface, and a global `scheme` registry of named validators. Primitive validators include `tFloat`, `tInt`, `tBoolean`, `tString`, and `tBinary`. Combinators such as `tOptional`, `tArray`, `tObject`, `tEnum`, and `tChannel` allow composing complex validation schemas. The `findValidator` and `maybeFindValidator` functions look up validators by type, method, and kind (Initializer, Event, Params, Result).
Usage
Use Validator Primitives when building or extending protocol validation schemas, or when validating incoming protocol messages on the server or client side of the Playwright RPC channel.
Code Reference
Source Location
- Repository: Microsoft_Playwright
- File: packages/playwright-core/src/protocol/validatorPrimitives.ts
Signature
export class ValidationError extends Error {}
export type Validator = (arg: any, path: string, context: ValidatorContext) => any;
export type ValidatorContext = {
tChannelImpl: (names: '*' | string[], arg: any, path: string, context: ValidatorContext) => any;
binary: 'toBase64' | 'fromBase64' | 'buffer';
isUnderTest: () => boolean;
};
export const scheme: { [key: string]: Validator };
export function findValidator(type: string, method: string, kind: 'Initializer' | 'Event' | 'Params' | 'Result'): Validator;
export function maybeFindValidator(type: string, method: string, kind: 'Initializer' | 'Event' | 'Params' | 'Result'): Validator | undefined;
Import
import { ValidationError, findValidator, scheme, tOptional, tString, tInt } from '../protocol/validatorPrimitives';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| arg | any | Yes | The value to validate |
| path | string | Yes | Dot-separated path describing the location within the message for error reporting |
| context | ValidatorContext | Yes | Validation context providing channel implementation lookup and binary mode |
Outputs
| Name | Type | Description |
|---|---|---|
| result | any | The validated (and possibly coerced) value, or throws ValidationError |
Usage Examples
import { findValidator, scheme, tString, tOptional, tObject } from '../protocol/validatorPrimitives';
// Register a custom validator
scheme['MyTypeMyMethodParams'] = tObject({
url: tString,
timeout: tOptional(tInt),
});
// Look up and use a validator
const validator = findValidator('Page', 'goto', 'Params');
const validated = validator(params, 'params', context);