def __init__(self, name, value=None, schema=None): self.name = name self.value = value self.schema = schema try: self.schema['type'] except KeyError: msg = (_('Schema definition of "%(pname)s" must have a "type" ' 'attribute.') % dict(pname=self.name)) ExceptionCollector.appendException( InvalidSchemaError(message=msg)) if 'required' in self.schema: required = self.schema['required'] if not isinstance(required, bool): if required.lower() not in self.VALID_REQUIRED_VALUES: valid_values = ', '.join(self.VALID_REQUIRED_VALUES) msg = (_('Schema definition of "%(propname)s" has ' '"required" attribute with invalid value ' '"%(value1)s". The value must be one of ' '"%(value2)s".') % {"propname": self.name, "value1": required, "value2": valid_values}) ExceptionCollector.appendException( InvalidSchemaError(message=msg))
def __init__(self, property_name, property_type, constraint): super(Pattern, self).__init__(property_name, property_type, constraint) if not isinstance(self.constraint_value, self.valid_types): ExceptionCollector.appendException( InvalidSchemaError(message=_('The property "pattern" ' 'expects a string.'))) self.match = re.compile(self.constraint_value).match
def __init__(self, property_name, property_type, constraint): super(MaxLength, self).__init__(property_name, property_type, constraint) if not isinstance(self.constraint_value, self.valid_types): ExceptionCollector.appendException( InvalidSchemaError(message=_('The property "max_length" ' 'expects an integer.')))
def __init__(self, property_name, property_type, constraint): super(ValidValues, self).__init__(property_name, property_type, constraint) if not isinstance(self.constraint_value, collections.Sequence): ExceptionCollector.appendException( InvalidSchemaError(message=_('The property "valid_values" ' 'expects a list.')))
def __init__(self, property_name, property_type, constraint): super(InRange, self).__init__(property_name, property_type, constraint) if(not isinstance(self.constraint_value, collections.Sequence) or (len(constraint[self.IN_RANGE]) != 2)): ExceptionCollector.appendException( InvalidSchemaError(message=_('The property "in_range" ' 'expects a list.'))) for value in self.constraint_value: if not isinstance(value, self.valid_types): ExceptionCollector.appendException( InvalidSchemaError(_('The property "in_range" expects ' 'comparable values.'))) self.min = self.constraint_value[0] self.max = self.constraint_value[1]
def __init__(self, property_name, property_type, constraint): super(LessOrEqual, self).__init__(property_name, property_type, constraint) if not isinstance(self.constraint_value, self.valid_types): ExceptionCollector.appendException( InvalidSchemaError(message=_('The property "less_or_equal" ' 'expects comparable values.')))
def __init__(self, property_name, property_type, constraint): super(GreaterThan, self).__init__(property_name, property_type, constraint) if not isinstance(constraint[self.GREATER_THAN], self.valid_types): ExceptionCollector.appendException( InvalidSchemaError(message=_('The property "greater_than" ' 'expects comparable values.')))
def __init__(self, name, schema_dict): self.name = name if not isinstance(schema_dict, collections.Mapping): msg = (_('Schema definition of "%(pname)s" must be a dict.') % dict(pname=name)) ExceptionCollector.appendException(InvalidSchemaError(message=msg)) try: schema_dict['type'] except KeyError: msg = (_('Schema definition of "%(pname)s" must have a "type" ' 'attribute.') % dict(pname=name)) ExceptionCollector.appendException(InvalidSchemaError(message=msg)) self.schema = schema_dict self._len = None self.constraints_list = []
def __new__(cls, property_name, property_type, constraint): if cls is not Constraint: return super(Constraint, cls).__new__(cls) if(not isinstance(constraint, collections.Mapping) or len(constraint) != 1): ExceptionCollector.appendException( InvalidSchemaError(message=_('Invalid constraint schema.'))) for type in constraint.keys(): ConstraintClass = get_constraint_class(type) if not ConstraintClass: msg = _('Invalid property "%s".') % type ExceptionCollector.appendException( InvalidSchemaError(message=msg)) return ConstraintClass(property_name, property_type, constraint)
def __init__(self, property_name, property_type, constraint): super(InRange, self).__init__(property_name, property_type, constraint) if(not isinstance(self.constraint_value, collections.Sequence) or (len(constraint[self.IN_RANGE]) != 2)): ExceptionCollector.appendException( InvalidSchemaError(message=_('The property "in_range" ' 'expects a list.'))) msg = _('The property "in_range" expects comparable values.') for value in self.constraint_value: if not isinstance(value, self.valid_types): ExceptionCollector.appendException( InvalidSchemaError(message=msg)) # The only string we allow for range is the special value # 'UNBOUNDED' if(isinstance(value, str) and value != self.UNBOUNDED): ExceptionCollector.appendException( InvalidSchemaError(message=msg)) self.min = self.constraint_value[0] self.max = self.constraint_value[1]
def __init__(self, property_name, property_type, constraint): super(SchemaConstraint, self).__init__(property_name, property_type, constraint) if not isinstance(self.constraint_value, self.valid_types): ExceptionCollector.appendException( InvalidSchemaError( message=_('The "schema" constraint expects a string.') ) ) self.schema = json.loads(self.constraint_value) from jsonschema import Draft7Validator Draft7Validator.check_schema(self.schema)
def __init__(self, property_name, property_type, constraint): super(GreaterOrEqual, self).__init__(property_name, property_type, constraint) if not isinstance(self.constraint_value, self.valid_types): ExceptionCollector.appendException( InvalidSchemaError( message=_( "The property " '"greater_or_equal" expects ' "comparable values." ) ) )
def __init__(self, property_name, property_type, constraint): self.property_name = property_name self.property_type = property_type self.constraint_value = constraint[self.constraint_key] self.constraint_value_msg = self.constraint_value if self.property_type in scalarunit.ScalarUnit.SCALAR_UNIT_TYPES: self.constraint_value = self._get_scalarunit_constraint_value() # check if constraint is valid for property type if property_type not in self.valid_prop_types: msg = _('Property "%(ctype)s" is not valid for data type ' '"%(dtype)s".') % dict(ctype=self.constraint_key, dtype=property_type) ExceptionCollector.appendException(InvalidSchemaError(message=msg))
def __init__(self, property_name, property_type, constraint): super(JSONSchema, self).__init__(property_name, property_type, constraint) if not isinstance(self.constraint_value, self.valid_types): ExceptionCollector.appendException( InvalidSchemaError(message='The property "schema" expects a string.')) if isinstance(self.constraint_value, str): if os.path.isfile(self.constraint_value): with open(self.constraint_value) as fd: self.schema = json.load(fd) else: raise FileNotFoundError(f'JSON-schema not found in path: {self.constraint_value}') elif isinstance(self.constraint_value, dict): self.schema = self.constraint_value
def __init__(self, name, value=None, schema=None): self.name = name self.value = value self.schema = schema self._status = self.PROPERTY_STATUS_DEFAULT self._required = self.PROPERTY_REQUIRED_DEFAULT # Validate required 'type' property exists try: self.schema['type'] except KeyError: msg = (_('Schema definition of "%(pname)s" must have a "type" ' 'attribute.') % dict(pname=self.name)) ExceptionCollector.appendException(InvalidSchemaError(message=msg)) if self.schema: self._load_required_attr_from_schema() self._load_status_attr_from_schema()
def __init__(self, property_name, property_type, constraint): super(Enum, self).__init__(property_name, property_type, constraint) if not isinstance(self.constraint_value, self.valid_types): ExceptionCollector.appendException( InvalidSchemaError(message=f'The property "enum expects a list of dict.'))