Structural validation for dynamic / anonymous objects at runtime.
You can check basic types like strings and ints values. For example:
var validator:AnonStruct = new AnonStruct();
validator.valueString()
.allowEmpty()
.refuseNull()
.startsWith('This')
.setAllowedOptions(['This is an example', 'Something']);
trace(validator.pass('This is an example')); // true
trace(validator.pass('')); // true
trace(validator.pass(' \n ')); // true
trace(validator.pass('Something')); // false
trace(validator.pass('This test!')); // false
trace(validator.pass(null)); // false
trace(validator.pass(42)); // false
trace(validator.getErrors('This is an example')); // []
trace(validator.getErrors('')); // []
trace(validator.getErrors(' \n ')); // []
trace(validator.getErrors('Something')); // [AnonStructError:Value must starts with This]
trace(validator.getErrors('This test!')); // [AnonStructError:This test! is not allowed. Allowed values are: This is an example, Something.]
trace(validator.getErrors(null)); // [AnonStructError:Value cannot be null]
trace(validator.getErrors(42)); // [AnonStructError:Value must be String]
For complex objects, you can describe the expected data structure:
var validator:AnonStruct = new AnonStruct();
validator.propertyString('a')
.allowEmpty()
.allowNull();
validator.propertyInt('b')
.refuseNull()
.greaterOrEqualThan(10);
trace(validator.pass({a:'', b:10, c:[]})); // true
trace(validator.pass({b:10})); // true
trace(validator.pass({b:5})); // false
trace(validator.pass({c:5})); // false
AnonStruct also check children objects ...
validator.propertyObject('child')
.allowNull()
.setStruct(validator);
trace(validator.getErrors({a:'', b:10})); // []
trace(validator.getErrors({a:'', b:10, child:null})); // []
trace(validator.getErrors({a:'', b:10, child:{a:'x', b:5}})); // [AnonStructError:child.b: Value should be greater than or equal 10]
trace(validator.getErrors({a:'', b:10, child:{b:20, child:{b:null}}})); // [AnonStructError:child.child.b: Value cannot be null]
... and arrays.
validator.propertyArray('list')
.allowNull()
.setStruct(validator);
trace(validator.getErrors({a:'', b:10})); // []
trace(validator.getErrors({b:10, list:[]})); // []
trace(validator.getErrors({b:10, list:[{b:10}, {b:0}]})); // [AnonStructError:list.[1].b: Value should be greater than or equal 10]
It's easy to set custom validations:
typedef UserLoginData = {
var user:String;
var password:String;
}
class ValidateUserLoginData extends AnonStruct {
public function new() {
super();
this.propertyString('user')
.refuseEmpty()
.refuseNull()
.addValidation(this.checkEmail);
this.propertyString('password')
.refuseEmpty()
.refuseNull()
.minChar(6)
.addValidation(
function (value:String):Void {
var hasNumber:Bool = false;
for (i in 0 ... value.length)
if ('0123456789'.indexOf(value.charAt(i)) > -1) {
hasNumber = true;
break;
}
if (!hasNumber) throw 'The password must include numbers.';
}
);
}
private function checkEmail(value:String):Void {
var emailExpression:EReg = ~/[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z][A-Z][A-Z]?/i;
if (!emailExpression.match(value)) throw 'User must be a valid email address.';
}
}
class Test {
static public function test():Void {
var user:UserLoginData = {
user : 'someuser@somewhere.com',
password : 'pass1234'
}
var validator:ValidateUserLoginData = new ValidateUserLoginData();
trace(validator.getErrors(user)); // []
user = {
user : 'wrong_user',
password : 'pass error'
}
trace(validator.getErrors(user)); // [AnonStructError:user: user must be a valid email address. , AnonStructError:password: The password must include numbers.]
}
}
Checks if object to be tested is a bool
value.
Checks if object to be tested is a string
value.
Checks if object to be tested is ai int
value.
Checks if object to be tested is a float
value.
Checks if object to be tested is an array
value.
Checks if object to be tested is a date
value.
Checks if object to be tested is an object
value.
The object to be tested cannot be null
.
The object to be tested can be null
.
Checks if the object to be tested have has an int
property named prop
.
Checks if the object to be tested have has a float
property named prop
.
Checks if the object to be tested have has a string
property named prop
.
Checks if the object to be tested have has an object
property named prop
.
Checks if the object to be tested have has an array
property named prop
.
Checks if the object to be tested have has a date
property named prop
.
Checks if the object to be tested have has a bool
property named prop
.
Validate data
against the rules. If stopOnFirstError
is true, AnonStruct will stop at first error ocurrence.
Throws Array<AnonStructError>
if data
don't meet the requirements.
Validate data
against the rules and stops on first error.
Throws AnonStructError
if data
don't meet the requirements.
Validate data
against the rules and returns Array<AnonStructError>
. If no errors found, returns an empty array.
Validate data
against the rules and returns true
if validation succeeds, otherwise false
.