Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A thin wrapper around the AJV JSON Validator for Python

NotificationsYou must be signed in to change notification settings

jdthorpe/ajvpy

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.

Getting started

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.

Object Modification with AJV

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.

Plugin Modules

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"})

Creating Plugin Bundles

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-plugin

which 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"})

AJV Version

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 webpack

About

A thin wrapper around the AJV JSON Validator for Python

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp