Skip to content

Commit

Permalink
Merge pull request #83 from metosin/validator
Browse files Browse the repository at this point in the history
Public Swagger Spec validator
  • Loading branch information
ikitommi committed Jan 16, 2016
2 parents 26caaa2 + 7eb8db2 commit 52be849
Show file tree
Hide file tree
Showing 9 changed files with 1,923 additions and 47 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,23 @@
- Fixed generating Swagger path templates in cases where path parameter is followed
by an extension ([#82](https://github.com/metosin/ring-swagger/issues/82))

- Make the JSON Schema validator public: `ring.swagger.validator/validate`.

```clojure
(require '[ring.swagger.validator :as v])

(v/validate (rs/swagger-json {:paths {"/api/ping" {:get nil}}}))
; nil

(v/validate (rs/swagger-json {:pathz {"/api/ping" {:get nil}}}))
; ({:level "error"
; :schema {:loadingURI "#", :pointer ""}
; :instance {:pointer ""}
; :domain "validation"
; :keyword "additionalProperties"
; :message "object instance has properties which are not allowed by the schema: [\"pathz\"]", :unwanted ["pathz"]})
```

## 0.22.2 (13.1.2016)

**[compare](https://github.com/metosin/ring-swagger/compare/0.22.1...0.22.2)**
Expand Down
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,17 +174,16 @@ you can do the following:
## Validating the Swagger Spec
The generated full spec can be validated against the [Swagger JSON Schema](https://raw.githubusercontent.com/reverb/swagger-spec/master/schemas/v2.0/schema.json)
via tools like [scjsv](https://github.com/metosin/scjsv).
with the help of [scjsv](https://github.com/metosin/scjsv). **NOTE** currently loads the JSON Schema files for the internet,
from http://json-schema.org/draft-04/schema.
```clojure
(require '[scjsv.core :as scjsv])
(require '[ring.swagger.validator :as v])
(def validator (scjsv/validator (slurp "https://raw.githubusercontent.com/reverb/swagger-spec/master/schemas/v2.0/schema.json")))
(validator (rs/swagger-json {:paths {"/api/ping" {:get nil}}}))
(v/validate (rs/swagger-json {:paths {"/api/ping" {:get nil}}}))
; nil
(validator (rs/swagger-json {:pathz {"/api/ping" {:get nil}}}))
(v/validate (rs/swagger-json {:pathz {"/api/ping" {:get nil}}}))
; ({:level "error"
; :schema {:loadingURI "#", :pointer ""}
; :instance {:pointer ""}
Expand Down
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
[metosin/schema-tools "0.7.0"]
[prismatic/schema "1.0.4"]
[prismatic/plumbing "0.5.2"]
[metosin/scjsv "0.2.0"]
[clj-time "0.11.0"]
[org.tobereplaced/lettercase "1.0.0"]
[potemkin "0.4.3"]
Expand All @@ -22,7 +23,6 @@
:dependencies [[org.clojure/clojure "1.7.0"]
[midje "1.8.3"]
[ring-mock "0.1.5"]
[metosin/scjsv "0.2.0"]
[metosin/ring-swagger-ui "2.1.4-0"]
[javax.servlet/servlet-api "2.5"]
; Required when using with Java 1.6
Expand Down
150 changes: 150 additions & 0 deletions resources/ring/swagger/json-schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
{
"id": "http://json-schema.org/draft-04/schema#",
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "Core schema meta-schema",
"definitions": {
"schemaArray": {
"type": "array",
"minItems": 1,
"items": { "$ref": "#" }
},
"positiveInteger": {
"type": "integer",
"minimum": 0
},
"positiveIntegerDefault0": {
"allOf": [ { "$ref": "#/definitions/positiveInteger" }, { "default": 0 } ]
},
"simpleTypes": {
"enum": [ "array", "boolean", "integer", "null", "number", "object", "string" ]
},
"stringArray": {
"type": "array",
"items": { "type": "string" },
"minItems": 1,
"uniqueItems": true
}
},
"type": "object",
"properties": {
"id": {
"type": "string",
"format": "uri"
},
"$schema": {
"type": "string",
"format": "uri"
},
"title": {
"type": "string"
},
"description": {
"type": "string"
},
"default": {},
"multipleOf": {
"type": "number",
"minimum": 0,
"exclusiveMinimum": true
},
"maximum": {
"type": "number"
},
"exclusiveMaximum": {
"type": "boolean",
"default": false
},
"minimum": {
"type": "number"
},
"exclusiveMinimum": {
"type": "boolean",
"default": false
},
"maxLength": { "$ref": "#/definitions/positiveInteger" },
"minLength": { "$ref": "#/definitions/positiveIntegerDefault0" },
"pattern": {
"type": "string",
"format": "regex"
},
"additionalItems": {
"anyOf": [
{ "type": "boolean" },
{ "$ref": "#" }
],
"default": {}
},
"items": {
"anyOf": [
{ "$ref": "#" },
{ "$ref": "#/definitions/schemaArray" }
],
"default": {}
},
"maxItems": { "$ref": "#/definitions/positiveInteger" },
"minItems": { "$ref": "#/definitions/positiveIntegerDefault0" },
"uniqueItems": {
"type": "boolean",
"default": false
},
"maxProperties": { "$ref": "#/definitions/positiveInteger" },
"minProperties": { "$ref": "#/definitions/positiveIntegerDefault0" },
"required": { "$ref": "#/definitions/stringArray" },
"additionalProperties": {
"anyOf": [
{ "type": "boolean" },
{ "$ref": "#" }
],
"default": {}
},
"definitions": {
"type": "object",
"additionalProperties": { "$ref": "#" },
"default": {}
},
"properties": {
"type": "object",
"additionalProperties": { "$ref": "#" },
"default": {}
},
"patternProperties": {
"type": "object",
"additionalProperties": { "$ref": "#" },
"default": {}
},
"dependencies": {
"type": "object",
"additionalProperties": {
"anyOf": [
{ "$ref": "#" },
{ "$ref": "#/definitions/stringArray" }
]
}
},
"enum": {
"type": "array",
"minItems": 1,
"uniqueItems": true
},
"type": {
"anyOf": [
{ "$ref": "#/definitions/simpleTypes" },
{
"type": "array",
"items": { "$ref": "#/definitions/simpleTypes" },
"minItems": 1,
"uniqueItems": true
}
]
},
"allOf": { "$ref": "#/definitions/schemaArray" },
"anyOf": { "$ref": "#/definitions/schemaArray" },
"oneOf": { "$ref": "#/definitions/schemaArray" },
"not": { "$ref": "#" }
},
"dependencies": {
"exclusiveMaximum": [ "maximum" ],
"exclusiveMinimum": [ "minimum" ]
},
"default": {}
}
Loading

0 comments on commit 52be849

Please sign in to comment.