Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 1993179a0986479624fefeae55e9a95a1e825d3c @superstructor superstructor committed Oct 20, 2012
Showing with 559 additions and 0 deletions.
  1. +13 −0 Cakefile
  2. +373 −0 LICENSE
  3. +22 −0 README.md
  4. +1 −0 hal-schema.json
  5. +76 −0 hal-schema.yaml
  6. +16 −0 package.json
  7. +6 −0 test.coffee
  8. +46 −0 test.json
  9. +6 −0 test.rb
@@ -0,0 +1,13 @@
+{ _ } = require('underscore')
+fs = require('fs')
+path = require('path')
+yaml = require('js-yaml')
+
+task 'build', 'Build DaveJS HAL JSON Schema', (options) ->
+ fs.readFile(path.join(__dirname, 'hal-schema.yaml'), 'utf-8', (err, content) ->
+ throw err if err
+ json = JSON.stringify(yaml.load(content))
+ fs.writeFile(path.join(__dirname, 'hal-schema.json'), json, 'utf-8', (err) ->
+ throw err if err
+ )
+ )
373 LICENSE

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -0,0 +1,22 @@
+# DaveJS JSON Schema
+
+[JSON Schema](http://json-schema.org) for [JSON Hypermedia API Language](http://tools.ietf.org/html/draft-kelly-json-hal-03).
+
+Tested against [JSV JSON Schema validator](https://github.com/garycourt/JSV). It should work with other compliant validators.
+
+## Limitations
+
+See issues.
+
+## Contributors
+
+The following are the major contributors of DaveJS:
+
+ * [Isaac Johnston](http://twitter.com/superstructor) (Creator)
+ * [SMX](https://smxemail.com)
+
+## License
+
+This Source Code Form is subject to the terms of the Mozilla Public License,
+v. 2.0. If a copy of the MPL was not distributed with this file, You can
+obtain one at http://mozilla.org/MPL/2.0/.
@@ -0,0 +1 @@
+{"id":"resource","type":"object","properties":{"_links":{"type":"object","title":"Hyperlink","description":"Represents a hyperlink from the containing resource to a URI.","additionalProperties":{"type":[{"id":"link","type":"object","properties":{"href":{"type":"string","title":"URI of the target resource","description":"Either a URI [RFC3986] or URI Template [RFC6570] of the target resource."},"templated":{"type":"boolean","optional":true,"default":false,"title":"URI Template","description":"Is true when the link object's href property is a URI Template. Defaults to false."},"type":{"type":"string","pattern":"^(application|audio|example|image|message|model|multipart|text|video)\\/[a-zA-Z0-9!#\\$&\\.\\+-\\^_]{1,127}$","optional":true,"title":"Media type indication of the target resource","description":"When present, used as a hint to indicate the media type expected when dereferencing the target resource."},"name":{"type":"string","optional":true,"title":"Secondary key","description":"When present, may be used as a secondary key for selecting link objects that contain the same relation type."},"profile":{"type":"string","format":"uri","optional":true,"title":"Additional semantics of the target resource","description":"A URI that, when dereferenced, results in a profile to allow clients to learn about additional semantics (constraints, conventions, extensions) that are associated with the target resource representation, in addition to those defined by the HAL media type and relations."},"title":{"type":"string","optional":true,"title":"Human-readable identifier","description":"When present, is used to label the destination of a link such that it can be used as a human-readable identifier (e.g. a menu entry) in the language indicated by the Content-Language header (if present)."},"hreflang":{"type":"string","pattern":"^([a-zA-Z]{2,3}(-[a-zA-Z]{3}(-[a-zA-Z]{3}){0,2})?(-[a-zA-Z]{4})?(-([a-zA-Z]{2}|[0-9]{3}))?(-([a-zA-Z0-9]{5,8}|[0-9][a-zA-Z0-9]{3}))*([0-9A-WY-Za-wy-z](-[a-zA-Z0-9]{2,8}){1,})*(x-[a-zA-Z0-9]{2,8})?)|(x-[a-zA-Z0-9]{2,8})|(en-GB-oed)|(i-ami)|(i-bnn)|(i-default)|(i-enochian)|(i-hak)|(i-klingon)|(i-lux)|(i-mingo)|(i-navajo)|(i-pwn)|(i-tao)|(i-tay)|(i-tsu)|(sgn-BE-FR)|(sgn-BE-NL)|(sgn-CH-DE)|(art-lojban)|(cel-gaulish)|(no-bok)|(no-nyn)|(zh-guoyu)|(zh-hakka)|(zh-min)|(zh-min-nan)|(zh-xiang)$","optional":true,"title":"Language indication of the target resource [RFC5988]","description":"When present, is a hint in RFC5646 format indicating what the language of the result of dereferencing the link should be. Note that this is only a hint; for example, it does not override the Content-Language header of a HTTP response obtained by actually following the link."}},"additionalProperties":false},{"type":"array","items":[{"$ref":"link"}],"uniqueItems":true,"additionalProperties":false}]},"optional":true},"_embedded":{"type":"object","additionalProperties":{"type":[{"$ref":"resource"},{"type":"array","items":[{"$ref":"resource"}],"uniqueItems":true,"additionalProperties":false}]},"optional":true}}}
@@ -0,0 +1,76 @@
+---
+ id: "resource"
+ type: "object"
+ properties:
+ _links:
+ type: "object"
+ title: "Hyperlink"
+ description: "Represents a hyperlink from the containing resource to a URI."
+ additionalProperties:
+ type:
+ -
+ id: "link"
+ type: "object"
+ properties:
+ href:
+ type: "string"
+ title: "URI of the target resource"
+ description: "Either a URI [RFC3986] or URI Template [RFC6570] of the target resource."
+ templated:
+ type: "boolean"
+ optional: true
+ default: false
+ title: "URI Template"
+ description: "Is true when the link object's href property is a URI Template. Defaults to false."
+ type:
+ type: "string"
+ pattern: "^(application|audio|example|image|message|model|multipart|text|video)\\/[a-zA-Z0-9!#\\$&\\.\\+-\\^_]{1,127}$"
+ optional: true
+ title: "Media type indication of the target resource"
+ description: "When present, used as a hint to indicate the media type expected when dereferencing the target resource."
+ name:
+ type: "string"
+ optional: true
+ title: "Secondary key"
+ description: "When present, may be used as a secondary key for selecting link objects that contain the same relation type."
+ profile:
+ type: "string"
+ format: "uri"
+ optional: true
+ title: "Additional semantics of the target resource"
+ description: "A URI that, when dereferenced, results in a profile to allow clients to learn about additional semantics (constraints, conventions, extensions) that are associated with the target resource representation, in addition to those defined by the HAL media type and relations."
+ title:
+ type: "string"
+ optional: true
+ title: "Human-readable identifier"
+ description: "When present, is used to label the destination of a link such that it can be used as a human-readable identifier (e.g. a menu entry) in the language indicated by the Content-Language header (if present)."
+ hreflang:
+ type: "string"
+ pattern: "^([a-zA-Z]{2,3}(-[a-zA-Z]{3}(-[a-zA-Z]{3}){0,2})?(-[a-zA-Z]{4})?(-([a-zA-Z]{2}|[0-9]{3}))?(-([a-zA-Z0-9]{5,8}|[0-9][a-zA-Z0-9]{3}))*([0-9A-WY-Za-wy-z](-[a-zA-Z0-9]{2,8}){1,})*(x-[a-zA-Z0-9]{2,8})?)|(x-[a-zA-Z0-9]{2,8})|(en-GB-oed)|(i-ami)|(i-bnn)|(i-default)|(i-enochian)|(i-hak)|(i-klingon)|(i-lux)|(i-mingo)|(i-navajo)|(i-pwn)|(i-tao)|(i-tay)|(i-tsu)|(sgn-BE-FR)|(sgn-BE-NL)|(sgn-CH-DE)|(art-lojban)|(cel-gaulish)|(no-bok)|(no-nyn)|(zh-guoyu)|(zh-hakka)|(zh-min)|(zh-min-nan)|(zh-xiang)$"
+ optional: true
+ title: "Language indication of the target resource [RFC5988]"
+ description: "When present, is a hint in RFC5646 format indicating what the language of the result of dereferencing the link should be. Note that this is only a hint; for example, it does not override the Content-Language header of a HTTP response obtained by actually following the link."
+ additionalProperties: false
+ -
+ type: "array"
+ items:
+ -
+ $ref: "link"
+ uniqueItems: true
+ additionalProperties: false
+ optional: true
+ _embedded:
+ type: "object"
+ additionalProperties:
+ type:
+ -
+ $ref: "resource"
+ -
+ type: "array"
+ items:
+ -
+ $ref: "resource"
+ uniqueItems: true
+ additionalProperties: false
+ optional: true
+
@@ -0,0 +1,16 @@
+{
+ "name": "dave.schema.json",
+ "version": "0.0.1",
+ "description": "JSON Schema for JSON Hypermedia API Language",
+ "main": "index.js",
+ "scripts": {
+ "test": "echo \"Error: no test specified\" && exit 1"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/DaveJS/dave.schema.json.git"
+ },
+ "author": "Isaac Johnston <isaac@superstruct.co>",
+ "license": "MPL2",
+ "readmeFilename": "README.md"
+}
@@ -0,0 +1,6 @@
+JSV = require('JSV').JSV
+json = require('./test.json')
+schema = require('./hal.json')
+env = JSV.createEnvironment()
+report = env.validate(json, schema)
+console.dir(report)
@@ -0,0 +1,46 @@
+{
+ "_links": {
+ "self": {
+ "href": "http://davejs.github.com/dave.schema.json/test.json"
+ },
+ "curie": {
+ "name": "acme",
+ "href": "http//example.com/relations/{rel}",
+ "templated": true
+ },
+ "acme:widgets": {
+ "href": "/widgets",
+ "title": "A widget",
+ "hreflang": "en",
+ "type": "application/hal+json"
+ }
+ },
+ "_embedded": {
+ "acme:example": {
+ "_links": {
+ "self": {
+ "href": "http://davejs.github.com/dave.schema.json/test.json#example-embedded-resource"
+ }
+ },
+ "_embedded": {
+ "acme:nested": [
+ {
+ "_links": {
+ "self": {
+ "href": "http://davejs.github.com/dave.schema.json/test.json#example-nested-1"
+ }
+ }
+ },
+ {
+ "_links": {
+ "self": {
+ "href": "http://davejs.github.com/dave.schema.json/test.json#example-nested-2"
+ }
+ }
+ }
+ ]
+ }
+ }
+ },
+ "example": 42
+}
@@ -0,0 +1,6 @@
+require 'rubygems'
+require 'json'
+require 'jsonschema'
+schema = File.open(File.join(File.dirname(__FILE__), "hal.json"), "rb"){|f| JSON.parse(f.read)}
+data = File.open(File.join(File.dirname(__FILE__), "test.json"), "rb"){|f| JSON.parse(f.read)}
+JSON::Schema.validate(data, schema)

0 comments on commit 1993179

Please sign in to comment.