Skip to content
This repository has been archived by the owner on Feb 16, 2021. It is now read-only.

Commit

Permalink
Merge pull request #36 from gcanti/12
Browse files Browse the repository at this point in the history
fix #12
  • Loading branch information
gcanti committed Jan 14, 2016
2 parents f4b695f + 98e824b commit 069a023
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
**Note**: Gaps between patch versions are faulty/broken releases.
**Note**: A feature tagged as Experimental is in a high state of flux, you're at risk of it changing without notice.

## 2.3.0

- **New Feature**
- add `strict` option: no additional properties are allowed while validating structs, fix #12

## 2.2.0

- **New Feature**
Expand Down
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ validate(value, type, [options]) -> ValidationResult
- `value` the value to validate
- `type` a type defined with the [tcomb](https://github.com/gcanti/tcomb) library
- `options` (optional) is an object with the following keys
- `path` path prefix for validation
- `context` passed to `getValidationErrorMessage` (useful for i18n)
- `path: Array<string | number>` path prefix for validation
- `context: any` passed to `getValidationErrorMessage` (useful for i18n)
- `strict: boolean` (default `false`) if `true` no additional properties are allowed while validating structs

returns a `ValidationResult` object containing the result of the validation

Expand Down Expand Up @@ -139,7 +140,7 @@ validate(null, Point).isValid(); // => false
validate({x: 0}, Point).isValid(); // => false, y is missing
validate({x: 0, y: 'a'}, Point).isValid(); // => false, y is not a number
validate({x: 0, y: 0}, Point).isValid(); // => true

validate({x: 0, y: 0, z: 0}, Point, { strict: true }).isValid(); // => false, no additional properties are allowed
```

## Lists and tuples
Expand Down Expand Up @@ -395,8 +396,9 @@ validate(1, t.String).firstError(); // => 'value is `1`, should be a `Str`'
- `value` the value to validate
- `type` a type defined with the tcomb library
- `options` (optional) is an object with the following keys
- `path` path prefix for validation
- `context` passed to `getValidationErrorMessage` (useful for i18n)
- `path: Array<string | number>` path prefix for validation
- `context: any` passed to `getValidationErrorMessage` (useful for i18n)
- `strict: boolean` (default `false`) if `true` no additional properties are allowed while validating structs

# Tests

Expand Down
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@ validators.struct = function validateStruct(x, type, path, options) {
ret.errors = ret.errors.concat(prop.errors);
}
}
if (options.strict) {
for (var field in x) {
if (x.hasOwnProperty(field) && !props.hasOwnProperty(field) && !t.Nil.is(x[field])) {
ret.errors.push(ValidationError.of(x[field], t.Nil, path.concat(field), options.context));
}
}
}
if (!ret.errors.length) {
ret.value = new type(ret.value);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tcomb-validation",
"version": "2.2.0",
"version": "2.3.0",
"description": "General purpose validation library for JavaScript",
"main": "index.js",
"scripts": {
Expand Down
17 changes: 17 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,23 @@ describe('validate(value, type, [options])', function () {
assert.deepEqual(validate('abc', ShortString, {context: context, path: ['a']}).firstError().path, ['a']);
});

it('should handle a strict boolean', function () {
eq(validate({x: 0, y: 0}, Point, {strict: true}), success({x: 0, y: 0}));
eq(validate({x: 0, y: 0, z: 0}, Point, {strict: true}), failure(0, t.Nil, ['z'], 'Invalid value 0 supplied to /z: Nil', {x: 0, y: 0}));
eq(validate({x: 0, y: 0, z: null}, Point, {strict: true}), success({x: 0, y: 0}));
eq(validate({x: 0, y: 0, z: undefined}, Point, {strict: true}), success({x: 0, y: 0}));
});

it('should handle a strict boolean with nested structures', function () {
var InnerType = t.struct({point: Point});
var List = t.list(InnerType);
var T = t.subtype(List, function (x) {
return x.length >= 2;
});
eq(validate([{point: {x: 0, y: 0}}, {point: {x: 0, y: 0}}], T, {strict: true}), success([{point: {x: 0, y: 0}}, {point: {x: 0, y: 0}}]));
eq(validate([{point: {x: 0, y: 0, z: 0}}, {point: {x: 0, y: 0}}], T, {strict: true}), failure(0, t.Nil, [0, 'point', 'z'], 'Invalid value 0 supplied to /0/point/z: Nil', [{point: {x: 0, y: 0}}, {point: {x: 0, y: 0}}]));
});

});

});
Expand Down

0 comments on commit 069a023

Please sign in to comment.