def validate(self, value, messages=None): """Returns True if the given field value is valid, False otherwise. Validation error messages are appended to an optional messages array. """ valid = True primitive = value def log(msg): if messages is not None: messages.append(msg) if self.enum: if value not in self.enum.values(): valid = False flds = (self.name, str(value)) log("%s value '%s' not in allowed enumerated values." % flds) else: primitive = int(self.enum.keys()[self.enum.values().index(value)]) if self.type: if self.type.validate(primitive, messages, self.name) is False: valid = False return valid
def validate(self, value, messages=None): """Returns True if the given Argument value is valid, False otherwise. Validation error messages are appended to an optional messages array. """ valid = True primitive = value def log(msg): if messages is not None: messages.append(msg) if self.enum: if value not in self.enum.keys(): valid = False args = (self.name, str(value)) log("%s value '%s' not in allowed enumerated values." % args) else: primitive = int(self.enum[value]) if self.type: if self.type.validate(primitive, messages, self.name) is False: valid = False if self.range: if primitive < self.range[0] or primitive > self.range[1]: valid = False args = (self.name, str(primitive), self.range[0], self.range[1]) log("%s value '%s' out of range [%d, %d]." % args) return valid
def validate(self, value, messages=None, prefix=None): """validate(value[, messages[, prefix]]) -> True | False Validates the given value according to this PrimitiveType definition. Validation error messages are appended to an optional messages array, each with the optional message prefix. """ valid = False def log(msg): if messages is not None: if prefix is not None: tok = msg.split() msg = prefix + ' ' + tok[0].lower() + " " + " ".join( tok[1:]) messages.append(msg) if self.string: valid = type(value) is str else: if type(value) is str: log("String '%s' cannot be represented as a number." % value) elif type(value) not in (int, long, float): log("Value '%s' is not a primitive type." % str(value)) elif type(value) is float and not self.float: log("Float '%g' cannot be represented as an integer." % value) else: if value < self.min or value > self.max: args = (str(value), self.min, self.max) log("Value '%s' out of range [%d, %d]." % args) else: valid = True return valid