Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,25 @@ Defines a custom error code.

An example of `defaultMessage` might be: `"Incorrect moon (expected {expected}, got {actual}"`). This is filled out if a custom keyword returns a object `message` (see above). Translations will be used, if associated with the correct code name/number.

##### ValidatorContext

Create custom validation executor using `ValidatorContext`

````
var context = new ValidatorContext({}, true);
context.addSchema('', schema);
context.validateAll(data, schema, null, null, '');
if (banUnknownProperties) {
context.banUnknownProperties();
}
context.errors; // contains errors
context.missing; // contains missing schemas
````

##### ValidationError

Create custom validation error using `ValidationError`

## Demos

### Basic usage
Expand Down
5 changes: 3 additions & 2 deletions component.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
"scripts": [
"tv4.js",
"lang/de.js",
"lang/no-nb.js",
"lang/fr.js",
"lang/nb.js",
"lang/pl-PL.js",
"lang/pt-PT.js",
"lang/sv-SE.js"
],
"styles": [],
"files": []
}
}
12 changes: 12 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,18 @@ <h5 id="defineerror-codename-codenumber-defaultmessage-">defineError(codeName, c
<li><code>defaultMessage</code> is an error message template to use (assuming translations have not been provided for this code)</li>
</ul>
<p>An example of <code>defaultMessage</code> might be: <code>&quot;Incorrect moon (expected {expected}, got {actual}&quot;</code>). This is filled out if a custom keyword returns a object <code>message</code> (see above). Translations will be used, if associated with the correct code name/number.</p>
<h5 id="validatorcontext">ValidatorContext</h5>
<p>Create custom validation executor using <code>ValidatorContext</code></p>
<pre><code>var context = new ValidatorContext({}, true);
context.addSchema(&#39;&#39;, schema);
context.validateAll(data, schema, null, null, &#39;&#39;);
if (banUnknownProperties) {
context.banUnknownProperties();
}
context.errors; // contains errors
context.missing; // contains missing schemas</code></pre>
<h5 id="validationerror">ValidationError</h5>
<p>Create custom validation error using <code>ValidationError</code></p>
<h2 id="demos">Demos</h2>
<h3 id="basic-usage">Basic usage</h3>
<div class="content inline-demo" markdown="1" data-demo="demo1">
Expand Down
23 changes: 15 additions & 8 deletions source/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,19 +204,24 @@ function createApi(language) {
return result;
},
validateMultiple: function (data, schema, checkRecursive, banUnknownProperties) {
var context = new ValidatorContext(globalContext, true, languages[currentLanguage], checkRecursive, banUnknownProperties);
var result = {};

result.context = new ValidatorContext(globalContext, true, languages[currentLanguage], checkRecursive, banUnknownProperties);
if (typeof schema === "string") {
schema = {"$ref": schema};
}
context.addSchema("", schema);
context.validateAll(data, schema, null, null, "");
result.context.addSchema("", schema);
result.context.validateAll(data, schema, null, null, "");

if (banUnknownProperties) {
context.banUnknownProperties();
result.context.banUnknownProperties();
}
var result = {};
result.errors = context.errors;
result.missing = context.missing;

result.errors = result.context.errors;
result.missing = result.context.missing;
result.valid = (result.errors.length === 0);
delete result.context;

return result;
},
addSchema: function () {
Expand Down Expand Up @@ -275,7 +280,9 @@ function createApi(language) {
normSchema: normSchema,
resolveUrl: resolveUrl,
getDocumentUri: getDocumentUri,
errorCodes: ErrorCodes
errorCodes: ErrorCodes,
ValidatorContext: ValidatorContext,
ValidationError: ValidationError
};
return api;
}
Expand Down
23 changes: 15 additions & 8 deletions tv4.js
Original file line number Diff line number Diff line change
Expand Up @@ -1540,19 +1540,24 @@ function createApi(language) {
return result;
},
validateMultiple: function (data, schema, checkRecursive, banUnknownProperties) {
var context = new ValidatorContext(globalContext, true, languages[currentLanguage], checkRecursive, banUnknownProperties);
var result = {};

result.context = new ValidatorContext(globalContext, true, languages[currentLanguage], checkRecursive, banUnknownProperties);
if (typeof schema === "string") {
schema = {"$ref": schema};
}
context.addSchema("", schema);
context.validateAll(data, schema, null, null, "");
result.context.addSchema("", schema);
result.context.validateAll(data, schema, null, null, "");

if (banUnknownProperties) {
context.banUnknownProperties();
result.context.banUnknownProperties();
}
var result = {};
result.errors = context.errors;
result.missing = context.missing;

result.errors = result.context.errors;
result.missing = result.context.missing;
result.valid = (result.errors.length === 0);
delete result.context;

return result;
},
addSchema: function () {
Expand Down Expand Up @@ -1611,7 +1616,9 @@ function createApi(language) {
normSchema: normSchema,
resolveUrl: resolveUrl,
getDocumentUri: getDocumentUri,
errorCodes: ErrorCodes
errorCodes: ErrorCodes,
ValidatorContext: ValidatorContext,
ValidationError: ValidationError
};
return api;
}
Expand Down
2 changes: 1 addition & 1 deletion tv4.min.js

Large diffs are not rendered by default.