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

Already on GitHub? Sign in to your account

validates validator for amanda :P #34

Closed
Tapsi opened this Issue Mar 4, 2012 · 3 comments

Comments

Projects
None yet
3 participants

Tapsi commented Mar 4, 2012

Often I have a situation in a game, that a schema will be used by different other schemas. Like a position will be used by an unit, in a move way and so on. I know you can validate the object with more than one schema or simply write the conditions into every schema. But both for me is like an anti pattern and not update friendly...
So I wrote a validator and want to share it with you, because I think it could be useful for the standard distribution as well.

var amanda = require('./../lib/amanda.js');

amanda.addValidator('validates',function(property, propertyValue, validator, propertyValidators, callback) {

if( typeof validator === 'array' ){
// if validator value is an array then try to validate all schemas
for( var i=0,e=validator.length; i<e; i++ ) amanda.validate( propertyValue, validator[i], function(e){
return (e)? callback(e) : callback();
});
}
else{
amanda.validate( propertyValue, validator, function(e){
return (e)? callback(e) : callback();
});
}
});

var sPos = {
type:'object',
properties:{
x:{ type:'number', required:true },
y:{ type:'number', required:true }
}
};

var positions = {
type:'array',
items:{
validates:sPos
}
};

var dPos = {
type:'object',
properties:{
p1:{
validates:sPos,
required:true
},
p2:{
validates:sPos,
required:true
}
}
}

console.log("starting");

amanda.validate( { x:0,y:0 }, sPos , function(e){
if(e) throw Error("failed");
console.log("t1 success");
});

amanda.validate( [{ x:0,y:0 },{ x:0,y:0 },{ x:0,y:0 },{ x:0,y:0 }], positions , function(e){
if(e) throw Error("failed");
console.log("t2 success");
});

amanda.validate( { p1:{ x:0,y:0 }, p2:{ x:0,y:0 } }, dPos, function(e){
if(e) throw Error("failed");
console.log("t3 success");
});

Are you trying to do multi level schema validation?

I updated your second and third examples and added another.

var amanda = require('./../lib/amanda.js');

var sPos = {
  required: true,
  type:'object',
  properties:{
    x:{ type:'number', required:true },
    y:{ type:'number', required:true }
  }
};

var positionsNew = {
  type :'array',
  items: sPos
};

var dPosNew = {
  type:'object',
  properties:{
    p1:sPos,
    p2:sPos
  }
}

console.log("starting");

amanda.validate( [{ x:0,y:0 },{ x:0,y:0 },{ x:0,y:0 },{ x:0,y:0 }], positionsNew , function(e){
  if(e) throw Error("failed");
  console.log("updated t2 success");
});

amanda.validate( { p1:{ x:0,y:0 }, p2:{ x:0,y:0 } }, dPosNew, function(e){
  if(e) throw Error("failed");
  console.log("updated t3 success");
});


var threeLevelSchema = {
  type: 'object',
  properties: {
    levelOne: {
      type: 'object',
      properties: {
        levelOne: {
          type: 'array',
          items: {
            type: 'object',
            properties: sPos
          }
        }
      }
    }
  }
}

amanda.validate(
  {
    levelOne: {
      levelTwo: [
        {x: 0, y: 0},
        {x: 0, y: 0}
      ]
    }
  }, 
  threeLevelSchema,
  function(e){
    if(e) throw Error("failed");
    console.log("threeLevelSchema success");  
  }
);

Tapsi commented Mar 13, 2012

Ah noo, how painful. Yep it's more clear.. why I didn't see it? Shame for me ...
Thanks for the tip =D

Owner

Baggz commented Mar 15, 2012

Also, in version 0.4.0 all attributes have an access to the internal API (via this keyword). You should consider one of the following methods.

@Baggz Baggz closed this Mar 15, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment