Package: kohi
kohiA powerfull schema validatorInstalationVia Poetry:poetryaddkohiVia PIP:pipinstallkohiVia GitHub (recommended only in dev env):gitclonehttps://github.com/natanfeitosa/kohi.git&&cdkohi&&pipinstall.QuickstartTo validate a type you can import your schema validator fromkohiorfrom kohi.<type> import <type>Schemae.g.Let's check if a person's date of birth is a positive integer less than the current date — 2023 — and greater than or equal to 2005fromkohiimportNumberSchema# from kohi.number import NumberScheman=NumberSchema().int().positive().lt(2023).gte(2005)print(n.validate(2005))# Trueprint(n.validate(2022))# Trueprint(n.validate(2004))# Falseprint(n.validate(2023))# False# You can see the errors generated in the last `validate` call just by accessing the `errors` property# print(n.errors) # ['number must be less than 2022']Validatorskohi.base.BaseSchemaOnly one base class for all schema validatorskohi.number.NumberSchemaorkohi.NumberSchemakohi.string.StringSchemaorkohi.StringSchemakohi.enum.EnumSchemaorkohi.EnumSchemakohi.dictionary.DictSchemaorkohi.DictSchemaMethodsBaseSchemaadd_validator(name, func): SelfAdd a custom data validatorvalidate(data): boolThe method to be called when we validate the schemareset(): NoneReset error listthrow(): SelfBy default no errors are thrown, but when this method is chained aValidationErrorwill be thrownadd_mutation(): SelfAdd a mutation function than will run after thevalidatemethod. P.S. Will only be executed in theparsemethodparse(data): typeof dataRun thevalidatemethod, the mutations and return a deep clone of datadefault(data): SelfSet a default value for when the validator receives None and you don't want to generate an erroroptional(): SelfAllow values Nonerequired(error_message=None): SelfMark the schema as required. Does not allow values NoneNumberSchemainherits fromBaseSchemaBy default validates int and floatfloat(): SelfValidate onlyfloatint(): SelfValidate onlyintlt(num): SelfValidates if the data is less thannumgt(num): SelfValidates if the data is greater thannumlte(num): SelfValidates if the data is less than or equal tonumgte(num): SelfValidates if the data is greater than or equal tonummin(num): SelfJust an alias forgte(num)max(num): SelfJust an alias forlte(nun)positive(): SelfJust an alias forgt(0)negative(): SelfJust an alias forlt(0)nonpositive(): SelfJust an alias forlte(0)nonnegative(): SelfJust an alias forgte(0)StringSchemainherits fromBaseSchemamin(min_length): SelfValidate if the data len is greater than or equal to min_lengthlength(length): SelfValidate if the data len equal to lengthmax(max_length): SelfValidate if the data len is less than or equal to max_lengthurl(): SelfValidate if the data is an urluuid(): SelfValidate if the data is a valid uuidstarts_with(text): SelfValidate if the data starts with textends_with(text): SelfValidate if the data ends with textEnumSchemainherits fromBaseSchemaone_of(opts): SelfValidate if the data is in optsnot_one_of(opts): SelfValidate that data is different from the values in optsDictSchemainherits fromBaseSchemaprops(**props): SelfDefines the structure of the dictionary in the format[key]: ClassValidatorDev envinstall development dependenciescheck types usingmypyrun all tests usingpytest