Giter VIP home page Giter VIP logo

json-schema's Introduction

json-schema

Build Status

json-schema is like XML schemas, but for JSON. It's haskell specific and has no relation to json-schema.org.

The package provides

  • An AST type Schema defining the structure of a JSON object.
  • A type class JSONSchema to define a schema for a type.
  • Combinators for defining schemas.
  • A module for validating Aeson values against schemas.
  • Built-in instances matching aeson's.
  • A Generics module matching aeson instances generated by generics-aeson

If you use another library for generating JSON instances (such as aeson's built-in generics/template haskell) or write aeson instances by hand you will not get matching JSONSchema instances if you use json-schema's generics module.

This package assumes that your ToJSON and FromJSON instances match.

Installation

This package is available on hackage

$ cabal install json-schema

Examples

see the examples folder or The test suite.

json-schema's People

Contributors

bergmark avatar hesselink avatar jcberentsen avatar sebastiaanvisser avatar

Stargazers

 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

json-schema's Issues

Arbitrary predicates

Hi, I just came across this package and I'm curious, is there any reason why schemas don't contain arbitrary predicates to run over the data?

For example, it looks like the only thing you can check on a string value is its length:

data Schema =
  ...
  | Value LengthBound

Why not this?

data Schema =
  ...
  | Value (Text -> Bool)

Thanks!

Default value

Is there a way of specifying a default value?

More generally, is the idea to eventually support all of the json-schema spec?

Derive schemas based on json instance structure rather than assume it's the same as the generic-aeson representation

This is a bigger project, I talked a bit to @MedeaMelana about using JsonGrammar which has an internal representation of conversions that we can use if the modules are exposed.

So we could implement this separately in json-schema for starters, just another function like gSchema that uses the JsonGrammar representation to make a schema.

As a second step we may want to look in to using JsonGrammar for generic-aeson, it means we'd lose the ability to just have one of the To/FromJSON instances but in practice it's fairly uncommon that you can only write one of them.

Haddock generation error with GHC 7.6.3

Unpacking to json-schema-0.6/
Resolving dependencies...
Configuring json-schema-0.6...
Building json-schema-0.6...
Preprocessing library json-schema-0.6...
[1 of 4] Compiling Data.JSON.Schema.Types ( src/Data/JSON/Schema/Types.hs, dist/build/Data/JSON/Schema/Types.o )
[2 of 4] Compiling Data.JSON.Schema.Combinators ( src/Data/JSON/Schema/Combinators.hs, dist/build/Data/JSON/Schema/Combinators.o )
[3 of 4] Compiling Data.JSON.Schema.Generic ( src/Data/JSON/Schema/Generic.hs, dist/build/Data/JSON/Schema/Generic.o )
[4 of 4] Compiling Data.JSON.Schema ( src/Data/JSON/Schema.hs, dist/build/Data/JSON/Schema.o )
In-place registering json-schema-0.6...
Preprocessing test suite 'json-schema-generic-aeson-tests' for
json-schema-0.6...
[1 of 1] Compiling Main             ( tests/Main.hs, dist/build/json-schema-generic-aeson-tests/json-schema-generic-aeson-tests-tmp/Main.o )
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package array-0.4.0.1 ... linking ... done.
Loading package deepseq-1.3.0.1 ... linking ... done.
Loading package bytestring-0.10.0.2 ... linking ... done.
Loading package containers-0.5.0.0 ... linking ... done.
Loading package text-1.1.1.3 ... linking ... done.
Loading package hashable-1.2.2.0 ... linking ... done.
Loading package scientific-0.3.2.1 ... linking ... done.
Loading package attoparsec-0.12.1.0 ... linking ... done.
Loading package dlist-0.7.1 ... linking ... done.
Loading package transformers-0.3.0.0 ... linking ... done.
Loading package mtl-2.1.3.1 ... linking ... done.
Loading package old-locale-1.0.0.5 ... linking ... done.
Loading package syb-0.4.2 ... linking ... done.
Loading package pretty-1.1.1.0 ... linking ... done.
Loading package template-haskell ... linking ... done.
Loading package time-1.4.0.1 ... linking ... done.
Loading package unordered-containers-0.2.4.0 ... linking ... done.
Loading package primitive-0.5.3.0 ... linking ... done.
Loading package vector-0.10.11.0 ... linking ... done.
Loading package aeson-0.7.0.6 ... linking ... done.
Loading package generic-deriving-1.6.3 ... linking ... done.
Loading package tagged-0.7.2 ... linking ... done.
Loading package generic-aeson-0.1.0.3 ... linking ... done.
Loading package json-schema-0.6 ... linking ... done.
Loading package regex-base-0.93.2 ... linking ... done.
Loading package regex-posix-0.95.2 ... linking ... done.
Loading package language-haskell-extract-0.2.4 ... linking ... done.
Loading package unix-2.6.0.1 ... linking ... done.
Loading package ansi-terminal-0.6.1.1 ... linking ... done.
Loading package stm-2.4.3 ... linking ... done.
Loading package async-2.0.1.5 ... linking ... done.
Loading package ansi-wl-pprint-0.6.7.1 ... linking ... done.
Loading package filepath-1.3.0.1 ... linking ... done.
Loading package directory-1.2.0.1 ... linking ... done.
Loading package process-1.1.0.2 ... linking ... done.
Loading package optparse-applicative-0.8.1 ... linking ... done.
Loading package parsec-3.1.5 ... linking ... done.
Loading package regex-tdfa-rc-1.1.8.3 ... linking ... done.
Loading package unbounded-delays-0.1.0.7 ... linking ... done.
Loading package tasty-0.8.1.3 ... linking ... done.
Loading package tasty-th-0.1.2 ... linking ... done.
Loading package HUnit-1.2.5.2 ... linking ... done.
Loading package tasty-hunit-0.8.0.1 ... linking ... done.
Linking dist/build/json-schema-generic-aeson-tests/json-schema-generic-aeson-tests ...
Building json-schema-0.6...
Preprocessing library json-schema-0.6...
In-place registering json-schema-0.6...
Preprocessing test suite 'json-schema-generic-aeson-tests' for
json-schema-0.6...
Running 1 test suites...
Test suite json-schema-generic-aeson-tests: RUNNING...
generic-aeson
  Main
    constructorWithoutFields:                                 OK
    record:                                                   OK
    recordWithFields:                                         OK
    constructorOneField:                                      OK
    constructorWithFields:                                    OK
    sumConstructorsWithField:                                 OK
    sumRecord:                                                OK
    sumRecordConstructorWithoutFields:                        OK
    sumConstructorWithoutFieldsConstructorWithFields:         OK
    sumConstructorWithoutFieldsConstructorWithRecursiveField: OK
    sum constructorWithoutFields record:                      OK
    recordListField:                                          OK
    constructorListField:                                     OK
    ConstructorSameTypedFields:                               OK
    RecordMaybeField:                                         OK
    constructorsWithoutFields:                                OK
    recordWithUnderscoredFields:                              OK

All 17 tests passed
Test suite json-schema-generic-aeson-tests: PASS
Test suite logged to:
dist/test/json-schema-0.6-json-schema-generic-aeson-tests.log
1 of 1 test suites (1 of 1 test cases) passed.
Running Haddock for json-schema-0.6...
Preprocessing library json-schema-0.6...
Warning: The documentation for the following packages are not installed. No
links will be generated to these packages: aeson-0.7.0.6, attoparsec-0.12.1.0,
dlist-0.7.1, generic-aeson-0.1.0.3, generic-deriving-1.6.3, hashable-1.2.2.0,
mtl-2.1.3.1, primitive-0.5.3.0, scientific-0.3.2.1, syb-0.4.2, tagged-0.7.2,
text-1.1.1.3, transformers-0.3.0.0, unordered-containers-0.2.4.0,
vector-0.10.11.0
Haddock coverage:
  75% (  6 /  8) in 'Data.JSON.Schema.Types'
  87% ( 13 / 15) in 'Data.JSON.Schema.Combinators'
 100% (  2 /  2) in 'Data.JSON.Schema.Generic'
Warning: Couldn't find .haddock for export Data.Proxy.Proxy
  50% (  2 /  4) in 'Data.JSON.Schema'
Warning: Data.JSON.Schema.Types: could not find link destinations for:
    Data.Text.Internal.Text Data.Text.Internal.Lazy.Text Data.Vector.Vector Data.HashMap.Base.HashMap Data.Aeson.Types.Internal.Value
Warning: Data.JSON.Schema.Combinators: could not find link destinations for:
    Data.Text.Internal.Text Data.Aeson.Types.Internal.Value
Warning: Data.JSON.Schema.Generic: could not find link destinations for:
    Data.JSON.Schema.Generic.GJSONSCHEMA Generics.Deriving.ConNames.ConNames Generics.Generic.IsEnum.GIsEnum
haddock: internal error: synifyKind

Export GJSONSchema class

The request is for the sake of type signatures.

E.g., if I want to have a function gMySchema = gSchemaWithSettings mySettings, then I cannot write its type signature (and get a warning)

Un-exposing Schema constructors

There are some things you can construct by using the constructors directly that don't make sense, aren't useful, or can be represented in several ways

Choice [] -- Doesn't match anything
Choice [foo,foo] = foo
Choice [Choice foo] = Choice [foo]
Tuple [] = Constant (Aeson.Array Vector.empty) = Array (LengthtBound (Just 0) (Just 0))
Null -- Same as Constant Aeson.Null
LengthBound (Just -2) (Just -1) -- Doesn't match anything

Some of these are hard to fix we can not (afaict) use a (Hash)Set for Choice since Aeson doesn't implement Ord or Hashable.

We can fix some of this by unexposing the constructors and only using smart constructors instead, <|> for Choice, <+> for Tuple, Value etc could have a Maybe Bound instead so you can create one by doing mkBound <$> foo 1 <*> foo 2.

I'm going to remove Null in the next release, but I'll let the rest air out for a bit.

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.