Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #34 from ixti/cast-booleans

Allow cast booleans
  • Loading branch information...
commit c31254d184261ef655165d5b6f60a760e528183b 2 parents df6a8b5 + 901709f
@pksunkara pksunkara authored
Showing with 54 additions and 16 deletions.
  1. +10 −0 lib/revalidator.js
  2. +44 −16 test/validator-test.js
View
10 lib/revalidator.js
@@ -241,6 +241,16 @@
if (('integer' === schema.type || 'number' === schema.type) && value == +value) {
value = +value;
}
+
+ if ('boolean' === schema.type) {
+ if ('true' === value || '1' === value || 1 === value) {
+ value = true;
+ }
+
+ if ('false' === value || '0' === value || 0 === value) {
+ value = false;
+ }
+ }
}
if (schema.format && options.validateFormats) {
View
60 test/validator-test.js
@@ -344,28 +344,56 @@ vows.describe('revalidator', {
"with <cast> option": {
topic: {
properties: {
- question: { type: "string" },
- answer: { type: "integer" }
+ answer: { type: "integer" },
+ is_ready: { type: "boolean" }
}
},
- "when the property is castable": {
- topic: function (schema) {
- return revalidator.validate({ answer: "42" }, schema, { cast: true });
+ "and <integer> property": {
+ "is castable string": {
+ topic: function (schema) {
+ return revalidator.validate({ answer: "42" }, schema, { cast: true });
+ },
+ "return an object with `valid` set to true": assertValid
},
- "return an object with `valid` set to true": assertValid
+ "is uncastable string": {
+ topic: function (schema) {
+ return revalidator.validate({ answer: "forty2" }, schema, { cast: true });
+ },
+ "return an object with `valid` set to false": assertInvalid
+ }
},
- "when the property is uncastable": {
- topic: function (schema) {
- return revalidator.validate({ answer: "forty2" }, schema, { cast: true });
+ "and <boolean> property": {
+ "is castable 'true/false' string": {
+ topic: function (schema) {
+ return revalidator.validate({ is_ready: "true" }, schema, { cast: true });
+ },
+ "return an object with `valid` set to true": assertValid
},
- "return an object with `valid` set to false": assertInvalid
- },
- "casting should respect property type": {
- topic: function (schema) {
- return revalidator.validate({ question: "42" }, schema, { cast: true });
+ "is castable '1/0' string": {
+ topic: function (schema) {
+ return revalidator.validate({ is_ready: "1" }, schema, { cast: true });
+ },
+ "return an object with `valid` set to true": assertValid
},
- "return an object with `valid` set to true": assertValid
+ "is castable `1/0` integer": {
+ topic: function (schema) {
+ return revalidator.validate({ is_ready: 1 }, schema, { cast: true });
+ },
+ "return an object with `valid` set to true": assertValid
+ },
+ "is uncastable string": {
+ topic: function (schema) {
+ return revalidator.validate({ is_ready: "not yet" }, schema, { cast: true });
+ },
+ "return an object with `valid` set to false": assertInvalid
+ },
+ "is uncastable number": {
+ topic: function (schema) {
+ return revalidator.validate({ is_ready: 42 }, schema, { cast: true });
+ },
+ "return an object with `valid` set to false": assertInvalid
+ }
}
- },
+ }
}
}).export(module);
Please sign in to comment.
Something went wrong with that request. Please try again.