def __init__(self, name, schema_dict, custom_defs=None): self.name = name self.schema = Schema(name, schema_dict) self.custom_defs = custom_defs or {} self._validate_field() self.validate_type(self.type)
def test_schema_none_description(self): tpl_snippet = ''' cpus: type: integer ''' schema = yamlparser.simple_parse(tpl_snippet) cpus_schema = Schema('cpus', schema['cpus']) self.assertEqual('', cpus_schema.description)
def __init__(self, property_name, value, schema_dict, custom_def=None): self.name = property_name self.value = value self.custom_def = custom_def self.entity = DataEntity(schema_dict['type'], self.value, self.custom_def, self.name) # the value_type will be the simple if the datatype was derived from one self.schema = Schema(property_name, schema_dict, self.entity.datatype.value_type) self._entity = None self._entry_schema_entity = None
def validate_entry(value, entry_schema, custom_def=None): '''Validate entries for map and list.''' schema = Schema(None, entry_schema) valuelist = value if isinstance(value, dict): valuelist = list(value.values()) for v in valuelist: DataEntity.validate_datatype(schema.type, v, schema.entry_schema, custom_def) if schema.constraints: for constraint in schema.constraints: constraint.validate(v) return value
def test_schema_dict(self): tpl_snippet = ''' cpus: type: integer description: Number of CPUs for the server. ''' schema = yamlparser.simple_parse(tpl_snippet) cpus_schema = Schema('cpus', schema['cpus']) self.assertEqual(len(cpus_schema), 2) self.assertEqual('integer', cpus_schema.type) self.assertEqual('Number of CPUs for the server.', cpus_schema.description) self.assertEqual(True, cpus_schema.required) self.assertIsNone(cpus_schema.default)
def validate(self): '''Validate the value by the definition of the datatype.''' # A datatype can not have both 'type' and 'properties' definitions. # If the datatype has 'type' definition if self.datatype.value_type: self.value = DataEntity.validate_datatype(self.datatype.value_type, self.value, None, self.custom_def) schema = Schema(self.property_name, self.datatype.defs) for constraint in schema.constraints: constraint.validate(self.value) # If the datatype has 'properties' definition else: if not isinstance(self.value, dict): ExceptionCollector.appendException( TypeMismatchError(what=self.value, type=self.datatype.type)) allowed_props = [] required_props = [] default_props = {} if self.schema: allowed_props = self.schema.keys() for name, prop_def in self.schema.items(): if prop_def.required: required_props.append(name) if prop_def.default: default_props[name] = prop_def.default # check allowed field for value_key in list(self.value.keys()): if value_key not in allowed_props: ExceptionCollector.appendException( UnknownFieldError(what=(_('Data value of type "%s"') % self.datatype.type), field=value_key)) # check default field for def_key, def_value in list(default_props.items()): if def_key not in list(self.value.keys()): self.value[def_key] = def_value # check missing field missingprop = [] for req_key in required_props: if req_key not in list(self.value.keys()): missingprop.append(req_key) if missingprop: ExceptionCollector.appendException( MissingRequiredFieldError( what=(_('Data value of type "%s"') % self.datatype.type), required=missingprop)) # check every field for name, value in list(self.value.items()): schema_name = self._find_schema(name) if not schema_name: continue prop_schema = Schema(name, schema_name) # check if field value meets type defined DataEntity.validate_datatype(prop_schema.type, value, prop_schema.entry_schema, self.custom_def) # check if field value meets constraints defined if prop_schema.constraints: for constraint in prop_schema.constraints: if isinstance(value, list): for val in value: constraint.validate(val) else: constraint.validate(value) return self.value
def __init__(self, property_name, value, schema_dict, custom_def=None): self.name = property_name self.value = value self.custom_def = custom_def self.schema = Schema(property_name, schema_dict)
def __init__(self, name, schema_dict): self.name = name self.schema = Schema(name, schema_dict) self._validate_field() self.validate_type(self.type)
def __init__(self, name, schema_dict): self.name = name self.schema = Schema(name, schema_dict)