Git Product home page Git Product logo

closchema's Introduction

Closchema

A clojure library that implements JSON schema validation.

This library is distributed under the MIT license (see LICENSE).

Clojars Project

Examples

(require '[closchema.core :as schema])
=> nil
(schema/validate {:type "array" :items {:type "string"}} ["1" "2"])
=> true
(schema/validate {:type "array" :items {:type "string"}} ["1" 2])
=> false
(schema/report-errors
  (schema/validate {:type "array" :items {:type "string"}} ["1" 2 {}]))
=> '(.. errors )

JSON Object Referencing (Schema extension)

This library implements the file-based part of referencing described here (at the end of the post):

http://www.sitepen.com/blog/2008/10/31/json-schema/

Right now, you can only specify filenames (not full URLs), and they must be located in your Java resources directory, as specified on the classpath. There is no ID-based referencing as of this readme.

With this extension, one can specify a reference to a file containing a schema using the key $ref when describing a property:

{
    "type" : "object",
    "properties" : {
        "id" : {"type" : "integer"},
        "address" : {"$ref" : "path/to/file"}
    }
}

Reading is lazy, so the schema is not read until we hit it at validation time. Files are read using slurp and clojure.data.json. Schema references are cached using memoize.

Inheritance with extends

extends is implemented as it is described here:

http://www.sitepen.com/blog/2009/09/02/json-namespacing/

Anything validating as :type "object" can use extends. The value of extends can be any valid schema, or any array of schemas. This specifies that, as part of its validation, this object must validate against the specified schema or schemas.

Thus, the following produces a schema specifying an :id that is an integer, and also specifying that an object must additionally validate against the schema in "file1.json".

{
    "type" : "object",
    "extends" : {"$ref" : "file1.json"},
    "properties" : {
        "id" : {"type" : "integer"}
    }
}

Multiple inheritance also has the expected semantics:

{
    "type" : "object",
    "extends" : [{"$ref" : "file1.json"}, {"$ref" : "file2.json"}],
    "properties" : {
        "id" : {"type" : "integer"}
    }
}

A word of caution: If :additionalProperties false is set in a "child" schema, nothing will validate against it unless the parent schema(s) is/are empty. That is, :additionalProperties takes precedence over :extends.

TODO

The entire spec http://tools.ietf.org/html/draft-zyp-json-schema-03 is not implemented yet. We'll gradually support more features as needed.

closchema's People

Contributors

aeronotix avatar ashenfad avatar cburgmer avatar charleslparker avatar cheesinglee avatar digiduke avatar diogok avatar jaor avatar jvah avatar marianoguerra avatar pedroteixeira avatar saebyn avatar trevorc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

closchema's Issues

Do something about "default" specifications

Schemas can specify a field as optional and give a default for it. It'd be nice to have a function that completes an instance of the schema according to that specification, filling in the blanks.

migrate to cheshire?

I've seen multiple projects migrate to cheshire, they say it's faster and since clojure.data.json breaks the API on 0.2.0 then it may make sense to make the switch now.

I can provide the pull request if it's ok for you.

Integers Don't Respond to Numeric Validation

Unless I'm very much mistaken, this should work better than it does:

=> (validate {:type "number" :minimum 0} 5)
true
=> (validate {:type "number" :minimum 0} -5)
false
=> (validate {:type "integer" :minimum 0} -5)
true

Sequences do not validate where vectors are expected

In at least some places, messages will not validate if the map contains a sequence rather than a vector. In particular, our dataset messages in bigmlcom/wintermute seem to not validate when :fields or :categories is a seq that has not been wrapped by vec.

See line notes from bigmlcom/wintermute#50, line 79. We should revisit this issue after this bug is fixed or debunked.

Funny Issues with "null" in objects

Specifically:

=> (validate {:type "null"} "a")
false
=> (validate {:type "null"} nil)
true
=> (validate {:type "object" :properties {:id {:type "null"}}} {:id nil})
false
=> (validate {:type "object" :properties {:id {:type "null"}}} {:id 4})
false
=> (validate {:type "object" :properties {:id {:type "number"}}} {:id 4})
true

Hmmmm...

pattern validation incorrect

By using the matches method on java strings to validate patterns, closchema effectively requires that the pattern match the entire string. However, this appears to be incorrect. Reading http://json-schema.org/latest/json-schema-validation.html - section 5.2.3.2 notes that "regular expressions are not anchored"; further section 3.3 gives as an example that the pattern "es" should match "expression". In closchema:

user=> (schema/validate {:type "string" :pattern "es"} "expression")
false

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.