Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

The ‘validate’ method now accepts JSON

  • Loading branch information...
commit 62c719f4976d40979c3918fcfad48616fcd25c15 1 parent a7315e7
@Baggz authored
Showing with 86 additions and 3 deletions.
  1. +86 −3 src/engines/json/validate.js
View
89 src/engines/json/validate.js
@@ -11,8 +11,91 @@ Validation.prototype.validate = function(instance, schema, callback) {
// Save a reference to the ‘this’
var self = this;
- return this.validateSchema(instance, schema, '', function(error) {
- return callback((self.Errors.length > 0) ? self.Errors : undefined);
- });
+ this.instance = instance;
+ this.schema = schema;
+
+ /**
+ * Basic Types
+ */
+ var basicTypes = [
+ 'string',
+ 'number',
+ 'function',
+ 'boolean',
+ 'integer',
+ 'int',
+ 'null'
+ ];
+
+ /**
+ * Object Types
+ */
+ var objectTypes = [
+ 'object',
+ 'array'
+ ];
+
+ /**
+ * CallbackProxy
+ */
+ var callbackProxy = function() {
+ if (self.errors.length !== 0) {
+ return callback(self.errors);
+ } else {
+ return callback();
+ }
+ };
+
+ /**
+ * {
+ * type: 'string',
+ * ...
+ * }
+ */
+ if (basicTypes.indexOf(schema.type) !== -1) {
+ return this.validateProperty(undefined, instance, schema, callbackProxy);
+ }
+
+ /**
+ * {
+ * type: 'object',
+ * ...
+ * }
+ */
+ if (objectTypes.indexOf(schema.type) !== -1) {
+ if (isString(instance)) {
+ try {
+ instance = JSON.parse(instance);
+ } catch(parseError) {
+ throw new Error(parseError);
+ }
+ }
+ return this.validateSchema(instance, schema, '', callbackProxy);
+ }
+
+ /**
+ * {
+ * type: ???,
+ * ...
+ * }
+ */
+ if (schema.type === 'any' || !schema.type) {
+
+ if (isString(instance)) {
+ try {
+ instance = JSON.parse(instance);
+ return this.validateSchema(instance, schema, '', callbackProxy);
+ } catch(parseError2) {
+
+ }
+ }
+
+ if (isObject(instance) || isArray (instance)) {
+ return this.validateSchema(instance, schema, '', callbackProxy);
+ }
+
+ return this.validateProperty(undefined, instance, schema, callbackProxy);
+
+ }
};
Please sign in to comment.
Something went wrong with that request. Please try again.