Custom error messages in JSON-\/\+Schema for Ajv validator

\href{https://travis-ci.org/epoberezkin/ajv-errors}{\texttt{ }} \href{http://badge.fury.io/js/ajv-errors}{\texttt{ }} \href{https://coveralls.io/github/epoberezkin/ajv-errors?branch=master}{\texttt{ }} \href{https://gitter.im/ajv-validator/ajv}{\texttt{ }}\hypertarget{md_sfcode_node_modules_ajv_errors_README_autotoc_md2680}{}\doxysection{Contents}\label{md_sfcode_node_modules_ajv_errors_README_autotoc_md2680}

\begin{DoxyItemize}
\item \href{\#install}{\texttt{ Install}}
\item \href{\#usage}{\texttt{ Usage}}
\begin{DoxyItemize}
\item \href{\#single-message}{\texttt{ Single message}}
\item \href{\#messages-for-keywords}{\texttt{ Messages for keywords}}
\item \href{\#messages-for-properties-and-items}{\texttt{ Messages for properties and items}}
\item \href{\#default-message}{\texttt{ Default message}}
\end{DoxyItemize}
\item \href{\#templates}{\texttt{ Templates}}
\item \href{\#options}{\texttt{ Options}}
\item \href{\#license}{\texttt{ License}}
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_ajv_errors_README_autotoc_md2681}{}\doxysection{Install}\label{md_sfcode_node_modules_ajv_errors_README_autotoc_md2681}

\begin{DoxyCode}{0}
\DoxyCodeLine{npm install ajv-\/errors }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_ajv_errors_README_autotoc_md2682}{}\doxysection{Usage}\label{md_sfcode_node_modules_ajv_errors_README_autotoc_md2682}
Add the keyword {\ttfamily error\+Messages} to Ajv instance\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{var Ajv = require('ajv');}
\DoxyCodeLine{var ajv = new Ajv(\{allErrors: true, jsonPointers: true\});}
\DoxyCodeLine{// Ajv options allErrors and jsonPointers are required}
\DoxyCodeLine{require('ajv-\/errors')(ajv /*, \{singleError: true\} */); }

\end{DoxyCode}


See \href{\#options}{\texttt{ Options}} below.\hypertarget{md_sfcode_node_modules_ajv_errors_README_autotoc_md2683}{}\doxysubsection{Single message}\label{md_sfcode_node_modules_ajv_errors_README_autotoc_md2683}
Replace all errors in the current schema and subschemas with a single message\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{var schema = \{}
\DoxyCodeLine{  type: 'object',}
\DoxyCodeLine{  required: ['foo'],}
\DoxyCodeLine{  properties: \{}
\DoxyCodeLine{    foo: \{ type: 'integer' \}}
\DoxyCodeLine{  \},}
\DoxyCodeLine{  additionalProperties: false,}
\DoxyCodeLine{  errorMessage: 'should be an object with an integer property foo only'}
\DoxyCodeLine{\};}
\DoxyCodeLine{}
\DoxyCodeLine{var validate = ajv.compile(schema);}
\DoxyCodeLine{console.log(validate(\{foo: 'a', bar: 2\})); // false}
\DoxyCodeLine{console.log(validate.errors); // processed errors }

\end{DoxyCode}


Processed errors\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{[}
\DoxyCodeLine{  \{}
\DoxyCodeLine{    keyword: 'errorMessage',}
\DoxyCodeLine{    message: 'should be an object with an integer property foo only',}
\DoxyCodeLine{    // ...}
\DoxyCodeLine{    params: \{}
\DoxyCodeLine{      errors: [}
\DoxyCodeLine{        \{ keyword: 'additionalProperties', dataPath: '' /* , ... */ \},}
\DoxyCodeLine{        \{ keyword: 'type', dataPath: '.foo' /* , ... */ \}}
\DoxyCodeLine{      ]}
\DoxyCodeLine{    \}}
\DoxyCodeLine{  \}}
\DoxyCodeLine{] }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_ajv_errors_README_autotoc_md2684}{}\doxysubsection{Messages for keywords}\label{md_sfcode_node_modules_ajv_errors_README_autotoc_md2684}
Replace errors for certain keywords in the current schema only\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{var schema = \{}
\DoxyCodeLine{  type: 'object',}
\DoxyCodeLine{  required: ['foo'],}
\DoxyCodeLine{  properties: \{}
\DoxyCodeLine{    foo: \{ type: 'integer' \}}
\DoxyCodeLine{  \},}
\DoxyCodeLine{  additionalProperties: false,}
\DoxyCodeLine{  errorMessage: \{}
\DoxyCodeLine{    type: 'should be an object', // will not replace internal "{}type"{} error for the property "{}foo"{}}
\DoxyCodeLine{    required: 'should have property foo',}
\DoxyCodeLine{    additionalProperties: 'should not have properties other than foo'}
\DoxyCodeLine{  \}}
\DoxyCodeLine{\};}
\DoxyCodeLine{}
\DoxyCodeLine{var validate = ajv.compile(schema);}
\DoxyCodeLine{console.log(validate(\{foo: 'a', bar: 2\})); // false}
\DoxyCodeLine{console.log(validate.errors); // processed errors }

\end{DoxyCode}


Processed errors\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{[}
\DoxyCodeLine{  \{}
\DoxyCodeLine{    // original error}
\DoxyCodeLine{    keyword: type,}
\DoxyCodeLine{    dataPath: '/foo',}
\DoxyCodeLine{    // ...}
\DoxyCodeLine{    message: 'should be integer'}
\DoxyCodeLine{  \},}
\DoxyCodeLine{  \{}
\DoxyCodeLine{    // generated error}
\DoxyCodeLine{    keyword: 'errorMessage',}
\DoxyCodeLine{    message: 'should not have properties other than foo',}
\DoxyCodeLine{    // ...}
\DoxyCodeLine{    params: \{}
\DoxyCodeLine{      errors: [}
\DoxyCodeLine{        \{ keyword: 'additionalProperties' /* , ... */ \}}
\DoxyCodeLine{      ]}
\DoxyCodeLine{    \},}
\DoxyCodeLine{  \}}
\DoxyCodeLine{] }

\end{DoxyCode}


For keywords \char`\"{}required\char`\"{} and \char`\"{}dependencies\char`\"{} it is possible to specify different messages for different properties\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{var schema = \{}
\DoxyCodeLine{  type: 'object',}
\DoxyCodeLine{  required: ['foo', 'bar'],}
\DoxyCodeLine{  properties: \{}
\DoxyCodeLine{    foo: \{ type: 'integer' \},}
\DoxyCodeLine{    bar: \{ type: 'string' \}}
\DoxyCodeLine{  \},}
\DoxyCodeLine{  errorMessage: \{}
\DoxyCodeLine{    type: 'should be an object', // will not replace internal "{}type"{} error for the property "{}foo"{}}
\DoxyCodeLine{    required: \{}
\DoxyCodeLine{      foo: 'should have an integer property "{}foo"{}',}
\DoxyCodeLine{      bar: 'should have a string property "{}bar"{}'}
\DoxyCodeLine{    \}}
\DoxyCodeLine{  \}}
\DoxyCodeLine{\}; }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_ajv_errors_README_autotoc_md2685}{}\doxysubsection{Messages for properties and items}\label{md_sfcode_node_modules_ajv_errors_README_autotoc_md2685}
Replace errors for properties / items (and deeper), regardless where in schema they were created\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{var schema = \{}
\DoxyCodeLine{  type: 'object',}
\DoxyCodeLine{  required: ['foo', 'bar'],}
\DoxyCodeLine{  allOf: [\{}
\DoxyCodeLine{    properties: \{}
\DoxyCodeLine{      foo: \{ type: 'integer', minimum: 2 \},}
\DoxyCodeLine{      bar: \{ type: 'string', minLength: 2 \}}
\DoxyCodeLine{    \},}
\DoxyCodeLine{    additionalProperties: false}
\DoxyCodeLine{  \}],}
\DoxyCodeLine{  errorMessage: \{}
\DoxyCodeLine{    properties: \{}
\DoxyCodeLine{      foo: 'data.foo should be integer >= 2',}
\DoxyCodeLine{      bar: 'data.bar should be string with length >= 2'}
\DoxyCodeLine{    \}}
\DoxyCodeLine{  \}}
\DoxyCodeLine{\};}
\DoxyCodeLine{}
\DoxyCodeLine{var validate = ajv.compile(schema);}
\DoxyCodeLine{console.log(validate(\{foo: 1, bar: 'a'\})); // false}
\DoxyCodeLine{console.log(validate.errors); // processed errors }

\end{DoxyCode}


Processed errors\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{[}
\DoxyCodeLine{  \{}
\DoxyCodeLine{    keyword: 'errorMessage',}
\DoxyCodeLine{    message: 'data.foo should be integer >= 2',}
\DoxyCodeLine{    dataPath: '/foo',}
\DoxyCodeLine{    // ...}
\DoxyCodeLine{    params: \{}
\DoxyCodeLine{      errors: [}
\DoxyCodeLine{        \{ keyword: 'minimum' /* , ... */ \}}
\DoxyCodeLine{      ]}
\DoxyCodeLine{    \},}
\DoxyCodeLine{  \},}
\DoxyCodeLine{  \{}
\DoxyCodeLine{    keyword: 'errorMessage',}
\DoxyCodeLine{    message: 'data.bar should be string with length >= 2',}
\DoxyCodeLine{    dataPath: '/bar',}
\DoxyCodeLine{    // ...}
\DoxyCodeLine{    params: \{}
\DoxyCodeLine{      errors: [}
\DoxyCodeLine{        \{ keyword: 'minLength' /* , ... */ \}}
\DoxyCodeLine{      ]}
\DoxyCodeLine{    \},}
\DoxyCodeLine{  \}}
\DoxyCodeLine{] }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_ajv_errors_README_autotoc_md2686}{}\doxysubsection{Default message}\label{md_sfcode_node_modules_ajv_errors_README_autotoc_md2686}
When the value of keyword {\ttfamily error\+Message} is an object you can specify a message that will be used if any error appears that is not specified by keywords/properties/items\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{var schema = \{}
\DoxyCodeLine{  type: 'object',}
\DoxyCodeLine{  required: ['foo', 'bar'],}
\DoxyCodeLine{  allOf: [\{}
\DoxyCodeLine{    properties: \{}
\DoxyCodeLine{      foo: \{ type: 'integer', minimum: 2 \},}
\DoxyCodeLine{      bar: \{ type: 'string', minLength: 2 \}}
\DoxyCodeLine{    \},}
\DoxyCodeLine{    additionalProperties: false}
\DoxyCodeLine{  \}],}
\DoxyCodeLine{  errorMessage: \{}
\DoxyCodeLine{    type: 'data should be an object',}
\DoxyCodeLine{    properties: \{}
\DoxyCodeLine{      foo: 'data.foo should be integer >= 2',}
\DoxyCodeLine{      bar: 'data.bar should be string with length >= 2'}
\DoxyCodeLine{    \},}
\DoxyCodeLine{    \_: 'data should have properties "{}foo"{} and "{}bar"{} only'}
\DoxyCodeLine{  \}}
\DoxyCodeLine{\};}
\DoxyCodeLine{}
\DoxyCodeLine{var validate = ajv.compile(schema);}
\DoxyCodeLine{console.log(validate(\{\})); // false}
\DoxyCodeLine{console.log(validate.errors); // processed errors }

\end{DoxyCode}


Processed errors\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{[}
\DoxyCodeLine{  \{}
\DoxyCodeLine{    keyword: 'errorMessage',}
\DoxyCodeLine{    message: 'data should be an object with properties "{}foo"{} and "{}bar"{} only',}
\DoxyCodeLine{    dataPath: '',}
\DoxyCodeLine{    // ...}
\DoxyCodeLine{    params: \{}
\DoxyCodeLine{      errors: [}
\DoxyCodeLine{        \{ keyword: 'required' /* , ... */ \},}
\DoxyCodeLine{        \{ keyword: 'required' /* , ... */ \}}
\DoxyCodeLine{      ]}
\DoxyCodeLine{    \},}
\DoxyCodeLine{  \}}
\DoxyCodeLine{] }

\end{DoxyCode}


The message in property {\ttfamily \+\_\+} of {\ttfamily error\+Message} replaces the same errors that would have been replaced if {\ttfamily error\+Message} were a string.\hypertarget{md_sfcode_node_modules_ajv_errors_README_autotoc_md2687}{}\doxysection{Templates}\label{md_sfcode_node_modules_ajv_errors_README_autotoc_md2687}
Custom error messages used in {\ttfamily error\+Message} keyword can be templates using \href{https://tools.ietf.org/html/rfc6901}{\texttt{ JSON-\/pointers}} or \href{http://tools.ietf.org/html/draft-luff-relative-json-pointer-00}{\texttt{ relative JSON-\/pointers}} to data being validated, in which case the value will be interpolated. Also see \href{https://gist.github.com/geraintluff/5911303}{\texttt{ examples}} of relative JSON-\/pointers.

The syntax to interpolate a value is {\ttfamily \$\{$<$pointer$>$\}}.

The values used in messages will be JSON-\/stringified\+:
\begin{DoxyItemize}
\item to differentiate between {\ttfamily false} and {\ttfamily \char`\"{}false\char`\"{}}, etc.
\item to support structured values.
\end{DoxyItemize}

Example\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{\{}
\DoxyCodeLine{  "{}type"{}: "{}object"{},}
\DoxyCodeLine{  "{}properties"{}: \{}
\DoxyCodeLine{    "{}size"{}: \{}
\DoxyCodeLine{      "{}type"{}: "{}number"{},}
\DoxyCodeLine{      "{}minimum"{}: 4}
\DoxyCodeLine{    \}}
\DoxyCodeLine{  \},}
\DoxyCodeLine{  "{}errorMessage"{}: \{}
\DoxyCodeLine{    "{}properties"{}: \{}
\DoxyCodeLine{      "{}size"{}: "{}size should be a number bigger or equal to 4, current value is \$\{/size\}"{}}
\DoxyCodeLine{    \}}
\DoxyCodeLine{  \}}
\DoxyCodeLine{\} }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_ajv_errors_README_autotoc_md2688}{}\doxysection{Options}\label{md_sfcode_node_modules_ajv_errors_README_autotoc_md2688}
Defaults\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{\{}
\DoxyCodeLine{  keepErrors: false,}
\DoxyCodeLine{  singleError: false}
\DoxyCodeLine{\} }

\end{DoxyCode}



\begin{DoxyItemize}
\item {\itshape keep\+Errors}\+: keep original errors. Default is to remove matched errors (they will still be available in {\ttfamily params.\+errors} property of generated error). If an error was matched and included in the error generated by {\ttfamily error\+Message} keyword it will have property {\ttfamily em\+Used\+: true}.
\item {\itshape single\+Error}\+: create one error for all keywords used in {\ttfamily error\+Message} keyword (error messages defined for properties and items are not merged because they have different data\+Paths). Multiple error messages are concatenated. Option values\+:
\begin{DoxyItemize}
\item {\ttfamily false} (default)\+: create multiple errors, one for each message
\item {\ttfamily true}\+: create single error, messages are concatenated using {\ttfamily \char`\"{}; \char`\"{}}
\item non-\/empty string\+: this string is used as a separator to concatenate messages
\end{DoxyItemize}
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_ajv_errors_README_autotoc_md2689}{}\doxysection{Supporters}\label{md_sfcode_node_modules_ajv_errors_README_autotoc_md2689}
\href{https://www.linkedin.com/in/rogerkepler/}{\texttt{ }} \href{https://www.linkedin.com/in/rogerkepler/}{\texttt{ Roger Kepler}}\hypertarget{md_sfcode_node_modules_ajv_errors_README_autotoc_md2690}{}\doxysection{License}\label{md_sfcode_node_modules_ajv_errors_README_autotoc_md2690}
\href{https://github.com/epoberezkin/ajv-errors/blob/master/LICENSE}{\texttt{ MIT}} 