def testWildcard(self) -> None: template = Validation({"*": "float", "hello": "integer"}) self.assertDictEqual( template.validate({ "test": "3.14" }).valuesAsDict, {"test": 3.14}) with self.assertRaisesRegex(ExceptionValidation, r"expects.*integer"): template.validate({"hello": "3.14"})
def testMinMaxWrongArgs(self) -> None: with self.assertRaisesRegex(ExceptionValidation, r"missing mandatory"): Validation({"test": "integer min"}) with self.assertRaisesRegex(ExceptionValidation, r"expects.*float"): Validation({"test": "integer min(str)"}) with self.assertRaisesRegex(ExceptionValidation, r"value not expected"): Validation({"test": "integer min(1,2)"})
def testFloat(self) -> None: template = Validation({"test": "float"}) template.validate({"test": "2"}) template.validate({"test": "2.1"}) template.validate({"test": "2.0"}) template.validate({"test": "-73"}) with self.assertRaisesRegex(ExceptionValidation, r"expects.*float"): template.validate({"test": "hsi"})
def testInteger(self) -> None: template = Validation({"test": "integer"}) template.validate({"test": "2"}) with self.assertRaisesRegex(ExceptionValidation, r"expects.*integer"): template.validate({"test": "2.1"}) template.validate({"test": "2.0"}) template.validate({"test": "-73"})
def addContract(self, contract: str) -> "ElementBuilder": """ Add a contract to the element. """ parsed = Validation.parse(contract) for kind, values in parsed.items(): Contract.add(element=self, kind=kind, values=values) return self
def testMinMaxLength(self) -> None: template = Validation({"test": "string min(2) max(10)"}) self.assertDictEqual( template.validate({ "test": "hello" }).valuesAsDict, {"test": "hello"}) with self.assertRaisesRegex(ExceptionValidation, r"shorter than"): template.validate({"test": "h"}) with self.assertRaisesRegex(ExceptionValidation, r"longer than"): template.validate({"test": "hello world"})
def testMinMax(self) -> None: template = Validation({"test": "integer min(2) max(10)"}) self.assertDictEqual( template.validate({ "test": "3" }).valuesAsDict, {"test": 3}) with self.assertRaisesRegex(ExceptionValidation, r"lower than"): template.validate({"test": "1"}) with self.assertRaisesRegex(ExceptionValidation, r"higher than"): template.validate({"test": "11"}) self.assertDictEqual( template.validate({ "test": "10" }).valuesAsDict, {"test": 10}) self.assertDictEqual( template.validate({ "test": "2" }).valuesAsDict, {"test": 2})
def _makeValueValidation( self, resolver: "Resolver", parameters: Parameters, contracts: Contracts) -> typing.Optional[Validation]: """ Generate the validation for the value by combining the type validation and the contract validation. """ # The validation comes from the direct underlying type, contract information # directly associated with this expression do not apply to the current validation. validationValue = contracts.validationForValue # Get the configuration value if any. if self.underlyingType is not None: underlyingType = resolver.getEntityResolved( self.underlyingType).assertValue(element=self.element) if underlyingType.isConfig: self.assertTrue( condition=not validationValue, message= "This expression has both a global contract and a configuration, this is not allowed." ) return self.makeValidationForValues(resolver=resolver, parameters=parameters) # If evaluates to true, meaning there is a contract for values, # it means there must be a single value. if validationValue: try: return Validation(schema={"0": validationValue}, args={"resolver": resolver}) except Exception as e: self.error(message=str(e)) # Validation is empty return Validation(schema={}, args={"resolver": resolver})
def testOutputReturn(self) -> None: template = Validation({"test": "integer"}) self.assertTrue(template.validate({"test": "2"}, output="return")) self.assertFalse(template.validate({"test": "2.1"}, output="return"))
def testString(self) -> None: template = Validation({"test": "string"}) template.validate({"test": "2"}) template.validate({"test": "dsdsd"})
def parse_number(string: str, args: typing.Any) -> float: Validation({"comma": "string"}).validate(args) if "comma" in args: string = string.replace(args["comma"], ".") return float(string)
def parser_date(string: str, args: typing.Any) -> datetime.datetime: Validation({"format": "string"}).validate(args) return datetime.datetime.strptime(string, args["format"])