Skip to content

Commit

Permalink
docs: templates in messages, closes #4
Browse files Browse the repository at this point in the history
  • Loading branch information
epoberezkin committed May 9, 2017
1 parent 324bd1b commit fb945c0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
30 changes: 30 additions & 0 deletions README.md
Expand Up @@ -165,6 +165,36 @@ Processed errors:
```


## 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}"
}
}
}
```


## License

[MIT](https://github.com/epoberezkin/ajv-errors/blob/master/LICENSE)
2 changes: 1 addition & 1 deletion SPEC.md
Expand Up @@ -78,7 +78,7 @@ console.log(ajv.errors);
Limitation - it is not possible to define error messages for $ref keyword.


## Templates // TODO
## Templates // DONE

Error messages used in errorMessage keyword can be templates using relative or absolute JSON pointers to data being validated, in which case the value will be interpolated. Primitive values (string, number, boolean, null) will be interpolated as is, structured values (array, object) will interpolated stable-stringified. Possibly, there can be a hook defined in options passed to `require('ajv-errors')` call to process interpolated values.

Expand Down

0 comments on commit fb945c0

Please sign in to comment.