Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

preValidateProperty + documentation #239

Merged
merged 3 commits into from
Dec 11, 2017
Merged
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
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,42 @@ them:
importNextSchema();
```

### Pre-Property Validation Hook
If some processing of properties is required prior to validation a function may be passed via the options parameter of the validate function. For example, say you needed to perform type coercion for some properties:

```const coercionHook = function (instance, property, schema, options, ctx) {
var value = instance[property];

// Skip nulls and undefineds
if (value === null || typeof value == 'undefined') {
return;
}

// If the schema declares a type and the property fails type validation.
if (schema.type && this.attributes.type.call(this, instance, schema, options, ctx.makeChild(schema, property))) {
var types = (schema.type instanceof Array) ? schema.type : [schema.type];
var coerced = undefined;

// Go through the declared types until we find something that we can
// coerce the value into.
for (var i = 0; typeof coerced == 'undefined' && i < types.length; i++) {
// If we support coercion to this type
if (lib.coercions[types[i]]) {
// ...attempt it.
coerced = lib.coercions[types[i]](value);
}
}
// If we got a successful coercion we modify the property of the instance.
if (typeof coerced != 'undefined') {
instance[property] = coerced;
}
}
}.bind(validator)

// And now, to actually perform validation with the coercion hook!
v.validate(instance, schema, { preValidateProperty: coercionHook });
```

## Tests
Uses [JSON Schema Test Suite](https://github.com/json-schema/JSON-Schema-Test-Suite) as well as our own tests.
You'll need to update and init the git submodules:
Expand Down
14 changes: 14 additions & 0 deletions lib/attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ validators.properties = function validateProperties (instance, schema, options,
var result = new ValidatorResult(instance, schema, options, ctx);
var properties = schema.properties || {};
for (var property in properties) {
if (typeof options.preValidateProperty == 'function') {
options.preValidateProperty(instance, property, properties[property], options, ctx);
}

var prop = (instance || undefined) && instance[property];
var res = this.validateSchema(prop, properties[property], options, ctx.makeChild(properties[property], property));
if(res.instance !== result.instance[property]) result.instance[property] = res.instance;
Expand Down Expand Up @@ -214,6 +218,11 @@ function testAdditionalProperty (instance, schema, options, ctx, property, resul
});
} else {
var additionalProperties = schema.additionalProperties || {};

if (typeof options.preValidateProperty == 'function') {
options.preValidateProperty(instance, property, additionalProperties, options, ctx);
}

var res = this.validateSchema(instance[property], additionalProperties, options, ctx.makeChild(additionalProperties, property));
if(res.instance !== result.instance[property]) result.instance[property] = res.instance;
result.importErrors(res);
Expand Down Expand Up @@ -242,6 +251,11 @@ validators.patternProperties = function validatePatternProperties (instance, sch
continue;
}
test = false;

if (typeof options.preValidateProperty == 'function') {
options.preValidateProperty(instance, property, patternProperties[pattern], options, ctx);
}

var res = this.validateSchema(instance[property], patternProperties[pattern], options, ctx.makeChild(patternProperties[pattern], property));
if(res.instance !== result.instance[property]) result.instance[property] = res.instance;
result.importErrors(res);
Expand Down