- Notifications
You must be signed in to change notification settings - Fork0
jdthorpe/ajvpy
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
A very thin wrapper around the awesomeAJV JSON validation library forJavaScript. Depends on thePyV8package. Just useimport Ajv from ajvpy, and away you go.
ajvpy alsoplays nicely with MongoEngine.
Note that the only real change is that you need to drop the JavaScript'snew andvar operators. See the officialAJVReadme for API documentation.
The fastest validation call:
fromajvpyimportAjvajv=Ajv()# options can be passed, e.g. {"allErrors": True}validate=ajv.compile(schema)valid=validate(data)ifnotvalid:print(validate.errors)
or with less code
# ...valid=ajv.validate(schema,data)ifnotvalid:print(ajv.errors)# ...
or
# ...ajv.addSchema(schema,'mySchema')valid=ajv.validate('mySchema',data)ifnotvalid:print(ajv.errorsText())# ...
SeeAPI andOptions for more details.
When using custom keywords which modify an object, the modified object maybe accessed via thelast attribute of anAjv orvalidator instance(after making the round-trip from python to JavaScript and back, of course):
importajvpyajv=ajvpy.Ajv()ajv.addKeyword('add-value', {"modifying":True,"type":"number",# note that ajvpy._eval(...) is not encouraged, see below"compile":ajvpy._eval("""(function (schema) { return (function (data, path, parent, key) { try { parent[key] += schema; return true; } catch (err) { return false; } }); })"""),"metaschema":{"type":"number"}, })x= {"value":1}ajv.validate({"type":"object","properties":{"value":{"type":"number","add-value":3 } }, },x)x#> {'value': 1}ajv.last#> {'value': 4}
** note thatajvpy._eval(...) evaluates JS code in the global scope andis not encouraged. Loading external UMD or CommonJS modules viaajvpy.load() is preferred.
CommonJS and UMD modules containing customkeywords andformats which would beloaded in NodeJS via:
// Create an Ajv instancevarAjv=requrire("ajv");varajv=newAjv();// Import the plugin modulevarmy_plugin=require("some-plugin-module");// Add the keywords and/or formats to the instancemy_plugin(ajv)// ormy_plugin(ajv,{"My":"options"})
and which are bundled into a stand alone bundle (seeCreating Plugin Bundles, below) can be loaded onto anajvpy instance like so:
# Create an Ajv instancefromajvpyimportAjvajv=Ajv()# Import and add the keywords and/or formats to the instanceajv.plugin("path/to/my/bundle.js")# orajv.plugin("path/to/my/bundle.js",{"My":"options"})
Or to re-use the plugin module, use:
fromajvpyimportloadmy_module=load("path/to/my/bundle.js")ajv.plugin(my_module)# orajv.plugin(my_module,{"My":"options"})
Plugin bundles can be created from node modules using one of the manyJavaScript bundlers, such aswebpack orBrowserify. The simplest way to bundle a NPMmodule containing an AJV plugin is to call:
cd path/to/ajvpynpm install my-favorite-ajv-pluginnpm run bundle -- my-favorite-ajv-pluginwhich will causewebpack to create a bundle of your plugin here atajvpy/plugins/my-favorite-ajv-plugin.js that can be used like so:
fromajvpyimportAjvajv=Ajv()# Import and add the keywords and/or formats to the instanceajv.plugin("my-favorite-ajv-plugin")# orajv.plugin("my-favorite-ajv-plugin",{"My":"options"})
The currently bundled version ofAJV is 5.2.0. To update the version ofAJV used byajvpy, you'll need to haveNodeJSinstalled on you machine, and then run:
cd path/to/ajvpynpm installnpm install ajv@latestnpm run webpackAbout
A thin wrapper around the AJV JSON Validator for Python
Resources
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Uh oh!
There was an error while loading.Please reload this page.