Package: vf
VF: Validation Functions (for py dicts.)VF is a simple tool for validating theschemaof dict-like Python objects. It is well suited for validating user-submitted forms, JSON requests, etc.InstallationInstall via pip:pip install vfOr downloadvf.pyinto your project directory.QuickstartCreate a validator function by supplying a schema:importvf;personValidator=vf.dictOf({"_id":vf.typeIs(str),"name":vf.dictOf({"first":vf.typeIs(str),"last":lambdax:type(x)isstr,}),"birth_timestamp":vf.typeIs(int),"gender":lambdax:xin["MALE","FEMALE","OTHER"],"parentIdList":vf.listOf(vf.typeIs(str)),"spouceId":lambdax:xisNoneortype(x)isstr,"childrenIdList":vf.listOf(vf.typeIs(str)),});vf.dictOf(.)returns a function for schema validation. (Above,personValidatoris the returned validation function.)Use the validator function to test if dicts are valid:personValidator({"_id":"00a3","name":{"first":"John","last":"Doe"},"birth_timestamp":318191400000,"gender":"MALE","parentIdList":["00a1","00a2"],"spouceId":"87b1","childrenIdList":["00a6","00a8"],});# Returns `True` => ValidIf validation fails, avf.ValidationErrorwill be raised:personValidator({"_id":"1c3f","name":{"first":"Jane","last":"Doe"},"birth_timestamp":"1990-01-01",# NOT int => Invalid"gender":"FEMALE","parentIdList":["1c3a","1c3b"],"spouceId":None,"childrenIdList":[],});# Raises `vf.ValidationError` => InvalidSchema Validation: How It WorksThe schema supplied tovf.dictOf(.)should be adict, where each value is a function. In the above example,vf.typeIs(str)is a shorthand forlambda x: type(x) is str.For validating a test-dicttDictagainst a schemaschema, we first ensure that the two have matching keys. Then, each value intDictis supplied to the corresponding function inschema. If that function returnsTrue(or a truthy value), the key/value pair is considered to be valid, invalid otherwise.VF includes a number of helpers for generating simple type-checking functions, which are documented below. But as a quick example,vf.typeIs(int)returns a function for checking if the type of it's argument is anint.vf.listOf(vf.typeIs(str))returns a function for checking for a list of strings.vf.allOf(vf.truthy, vf.typeIs(str))returns a function that checks for non-blank (i.e. truthy) strings.Write Your Ownlambdas!Please note that you aren't limited to VF's helpers. You can write anylambda(or reference adef'd function) in your schema. For example:instead ofvf.allOf(vf.truthy, vf.typeIs(str)),you may writelambda x: x and type(x) is str.More importantly, you will likely have custom validation logic that VF can't anticipate. For example, you may want to match integers between10and1500. In that case, you could use something like:lambda x: type(x) is int and 10 <= x <= 1500Quick PlugVF built and maintained by the folks atPolydojo, Inc., led bySumukh Barve. If your team is looking for a simple project management tool, please check out our latest product:BoardBell.com.Helper NomenclatureAt Polydojo, boolean variables are typically given names starting withis,did, etc. That is,isNone,isType,didConfirmetc. would typically be booleans. Thus, instead ofisType, VF includestypeIs(.), which is ahelperthat returns afunctionfor type-checking.List Of HelpersSimple functions:These helpers are simple functions that returnTrueorFalse.vf.truthy: Helper function for checking truthy-ness.vf.falsy: Helper function for checking falsy-ness.vf.noneIs: Helper for checkingNone.Helpers for generating functions:These helpers return type-checking functions, which in-turn returnTrueorFalse. Here-below, the phrase"Makesfunc(x)for ..."should be read as"Returns a function onxfor ..."vf.typeIs(typ): Makesfunc (x)for checkingtype(x) is typ.vf.typeIn(*typs): Makesfunc (x)for checkingtype(x) in typs.vf.instanceOf(*typs): Makesfunc (x)for checkingisinstance(x, typs).vf.patternIs(pattern): Makesfunc (s)for checkingsagainstpatternviare.matchorre.Pattern.match. (smay be astror a compiledre.Pattern.)allOf (*fns): Makesfunc (x)for checkingall(fn(x) for fn in fns).;anyOf (*fns): Makesfunc (x)for checkingany(fn(x) for fn in fns).listOf (fn, minLen=0): Makesfunc (li)for checkingisinstance(li, list),len(li) >= minLenandall(fn(x) for x in li).Dict Validation:And finally, of course, the dict validation function:dictOf(schema, extraKeysOk=False): Makesfunc (d)forvalidatingdagainstschema. Raisesvf.ValidationErrorif validation fails.IfextraKeysOk=Trueis passed, it is then acceptable ford.keys()to be a superset ofschema.keys(). Otherwise,d.keys()must exactly matchschema.keys()(irrespective of key-order).Testing & ContributingInstall pytest viapip install -U pytest. Run tests with:pytestIf you encounter a bug, please open an issue on GitHub; but if you find a security vulnerability, please emailsecurity@polydojo.cominstead.If you'd like to see a new feature or contribute code, please open a GitHub issue. We'd love to hear from you! Suggestions and code contributions will always be appreciated, big and small.LicensingCopyright (c) 2020 Polydojo, Inc.Software Licensing:The software is released "AS IS" under theMIT License, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED. Kindly seeLICENSE.txtfor more details.No Trademark Rights:The above software licensing termsdo notgrant any right in the trademarks, service marks, brand names or logos of Polydojo, Inc.