check-compiler-js
A TypeScript-first schema rule compiler and validation library.
Features
- Zero dependencies
- Type-safe schema definitions with full TypeScript inference
- Works in Node.js and browsers
- Tiny bundle size (minified+gzipped)
- Composable and reusable schema definitions
- Custom error messages per validation rule
- Support for nullable types and optional properties
Install
npm install check-compiler
Usage
String Schema
Creating a simple string schema:
import {compile, Rule, Violation} from 'check-compiler';
// define
const id: Rule<string> = {
type: 'string',
min: 8,
max: 8,
pattern: /^[a-z]+$/,
messages: {
'string pattern': 'Required to be lowercase alpha only.',
},
};
// compile
const checkId = compile(id);
// check
const input = 'abcdefgh';
const violations: Violation[] = [];
checkId(input, violations);
Object Schema
Creating an object schema:
import {compile, Rule, Violation} from 'check-compiler';
// type
type ResourceMap = {
cpu?: number | null;
memory?: number | null;
};
// define
const cpu: Rule<number | null> = {
type: 'number',
nullable: true,
min: 0,
max: 8000,
};
const memory: Rule<number | null> = {
type: 'number',
nullable: true,
min: 0,
max: 32768,
};
const resourceMap: Rule<ResourceMap | null> = {
type: 'object',
nullable: true,
properties: {
cpu,
memory,
},
};
// compile
const checkResourceMap = compile(resourceMap);
// check
const input: ResourceMap = {cpu: 1000, memory: 512};
const violations: Violation[] = [];
checkResourceMap(input, violations);
Validation with Exceptions
Defining validation by raising ValidationError:
import {compile, Check, Violation} from 'check-compiler';
export class ValidationError extends Error {
constructor(readonly details: Violation[]) {
super('There is one or more validation violations.');
}
}
export function makeAssetValid<T>(check: Check<T>): (input: T) => void {
return (input: T) => {
const violations: Violation[] = [];
check(input, violations);
if (violations.length > 0) {
throw new ValidationError(violations);
}
};
}
// compile
export const assertResourceMap = makeAssetValid(
compile<ResourceMap>({
type: 'object',
nullable: true,
properties: {
cpu: {type: 'number', nullable: true, min: 0, max: 8000},
memory: {type: 'number', nullable: true, min: 0, max: 32768},
},
}),
);
// assert
const input: ResourceMap = {cpu: 1000, memory: 512};
assertResourceMap(input);
Supported Types
The library supports comprehensive validation for the following TypeScript types:
Boolean
const rule: Rule<boolean> = {
type: 'boolean',
nullable: false, // optional
messages: {
'field not null': 'Custom message',
'boolean': 'Must be a boolean value',
},
};
Number & Integer
const rule: Rule<number> = {
type: 'number', // or 'integer'
nullable: false, // optional
min: 0, // optional
max: 100, // optional
messages: {
'number': 'Must be a number',
'integer': 'Must be an integer',
'number range': 'Must be between {min} and {max}',
},
};
String
const rule: Rule<string> = {
type: 'string',
nullable: false, // optional
min: 1, // minimum length, optional
max: 255, // maximum length, optional
pattern: /^[a-z]+$/, // regex pattern, optional
messages: {
'string': 'Must be a string',
'string min': 'Must be at least {min} characters',
'string pattern': 'Must match pattern',
},
};
Array
const rule: Rule<number[]> = {
type: 'array',
items: {type: 'number', min: 0},
nullable: false, // optional
min: 1, // minimum array length, optional
max: 10, // maximum array length, optional
messages: {
'array': 'Must be an array',
'array min': 'Must contain at least {min} items',
},
};
Object
type User = {
name: string;
age?: number;
};
const rule: Rule<User> = {
type: 'object',
nullable: false, // optional
properties: {
name: {type: 'string', min: 1},
age: {type: 'number', nullable: true, min: 0},
},
required: ['name'], // optional
additionalProperties: false, // optional, disallow unknown properties
minProperties: 1, // optional
maxProperties: 10, // optional
patternProperties: { // optional, for dynamic keys
'^[a-z]+
API Reference
compile<T>(rule: Rule<T>): Check<T>
Compiles a rule definition into a validation function.
- Parameters:
rule: The validation rule definition
- Returns: A
Check<T> function that validates input
Check<T>
Type definition for the compiled validation function.
type Check<T> = (input: T, violations: Violation[]) => void;
- Parameters:
input: The value to validate
violations: An array that will be populated with any validation errors
Violation
Structure of validation error objects:
type Violation = {
reason: string; // Machine-readable error type
message: string; // Human-readable error message
location?: string; // Property path (e.g., 'user.email')
args?: {[key: string]: unknown}; // Additional context
};
Violation Details Example
[
{
"location": "cpu",
"message": "The value must fall within the range 0 - 8000.",
"reason": "number range",
"args": {
"min": 0,
"max": 8000
}
}
]
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
: {type: 'string'},
},
};
API Reference
__INLINE_CODE_1__
Compiles a rule definition into a validation function.
- Parameters:
- __INLINE_CODE_2__: The validation rule definition
- Returns: A __INLINE_CODE_3__ function that validates input
__INLINE_CODE_4__
Type definition for the compiled validation function.
__CODE_BLOCK_9__- Parameters:
- __INLINE_CODE_5__: The value to validate
- __INLINE_CODE_6__: An array that will be populated with any validation errors
__INLINE_CODE_7__
Structure of validation error objects:
__CODE_BLOCK_10__Violation Details Example
__CODE_BLOCK_11__Contributing
Contributions are welcome! Please feel free to submit a Pull Request.