Custom validation keywords

Validator.addKeyword(name, definition) is used to register custom keywords with the underlying AJV instance.

It is for extending validation rules, such as isEven, maxWords, or startsWithPrefix. If you want to define reusable business field types such as tenant-id!, s('tenant-id!'), or s.tenantId('corp'), start with Custom DSL Types.

The current implementation is internally compatible with the object-based registration of AJV 8, so you can continue to use the two-parameter writing method of v1 without exposing deprecated warnings to the caller.

import { Validator } from 'schema-dsl/pure';
const validator = new Validator();
validator.addKeyword('isEven', {
  type: 'number',
  validate: (_schema, data) => data % 2 === 0
});

When to use it

NeedRecommended entry
Define a business field type once and use it from pure DSL, s('...'), and s.xxx()Custom DSL Types
Add a low-level validation keyword to JSON Schema validationValidator.addKeyword()
Register a group of business types for one project or frameworkFramework Integration
Package install, uninstall, and hook lifecyclePlugin Manager (Advanced)

For more AJV keyword definitions, please refer to the AJV official documentation.


Corresponding sample file

Example entry: add-keyword.ts Description: Cover the minimum registration and validation path of Validator.addKeyword(), and directly display the success/failure results.