# DSJSONSchemaValidation
**JSON Schema draft 4, draft 6 and draft 7 parsing and validation library written in Objective-C.**
[](https://github.com/Carthage/Carthage) []() []() []()
`DSJSONSchemaValidation` is a library that provides a set of classes for parsing [JSON Schema](http://json-schema.org/documentation.html) documents into native Objective-C objects and subsequently using them to validate JSON documents.
The main feature of the library is an ability to "compile" the schema into a network of objects that describe that schema, so that it could be cached and reused for validation of multiple JSON documents in a performant manner, similar to the way `NSRegularExpression` and `NSDateFormatter` classes are used. One of the possible use cases of this library could be early validation of JSON response received from a web service, based on expectations described within the app in a form of JSON Schema.
`DSJSONSchemaValidation` supports all validation keywords of JSON Schema draft 4, 6 and 7. It is also possible to extend the functionality of the library by defining custom keywords to be used with specific metaschema URIs and custom formats for the `format` validation keyword. Note that JSON Schema draft 3 is not supported at the moment. There are also a few important limitations, including usage of external schema references, listed under [Caveats and limitations](#caveats-and-limitations).
Based on https://github.com/vlas-voloshin/JSONSchemaValidation
## Requirements
`DSJSONSchemaValidation` currently supports building in Xcode 7.0 or later with ARC enabled. Minimum supported target platform versions are iOS 7.0, tvOS 9.0 and OS X 10.9. Library can be linked to Objective-C and Swift targets.
## Installation
### Carthage
1. Add the following line to your `Cartfile`:
```
github "dashevo/JSONSchemaValidation"
```
2. Follow the instructions outlined in [Carthage documentation](https://github.com/Carthage/Carthage/blob/master/README.md) to build and integrate the library into your app.
3. Import library header in your source files:
* Objective-C: `#import <DSJSONSchemaValidation/DSJSONSchemaValidation.h>`
* Swift: `import DSJSONSchemaValidation`
### CocoaPods
1. Add the following line to your `Podfile`:
```
pod 'DSJSONSchemaValidation'
```
2. Import library header in your source files:
* Objective-C: `#import <DSJSONSchemaValidation/DSJSONSchema.h>`
* Swift: `import DSJSONSchemaValidation`
### Framework (iOS 8.0+, tvOS and OS X)
1. Download and copy the repository source files into your project, or add it as a submodule to your git repository.
2. Drag&drop `DSJSONSchemaValidation.xcodeproj` into your project or workspace in Xcode.
3. In "General" tab of Project Settings → `Your Target`, you might find that Xcode has added a missing framework item in "Embedded Binaries". Delete it for now.
4. Still in "General" tab, add `DSJSONSchemaValidation.framework` from `DSJSONSchemaValidation-iOS`, `DSJSONSchemaValidation-tvOS` or `DSJSONSchemaValidation-OSX` target (depending on your target platform) to "Embedded Binaries". This should also add it to "Linked Frameworks and Libraries".
5. Import library header in your source files:
* Objective-C: `#import <DSJSONSchemaValidation/DSJSONSchemaValidation.h>`
* Swift: `import DSJSONSchemaValidation`
### Static library (iOS)
1. Download and copy the repository source files into your project, or add it as a submodule to your git repository.
2. Drag&drop `DSJSONSchemaValidation.xcodeproj` into your project or workspace in Xcode.
3. In "General" section of Project Settings → `Your Target`, you might find that Xcode has added a missing framework item in "Embedded Binaries". Delete it for now.
4. Still in "General" tab, add `libDSJSONSchemaValidation.a` to "Linked Frameworks and Libraries".
5. Add project path to `Your Target` → Build Settings → Header Search Paths (e.g. `"$(SRCROOT)/MyAwesomeProject/Vendor/DSJSONSchemaValidation/"`).
6. Add `-ObjC` flag to `Your Target` → Build Settings → Other Linker Flags to ensure that categories defined in the static library are loaded.
7. Import library header in your source files:
* Objective-C: `#import <DSJSONSchemaValidation/DSJSONSchema.h>`
* Swift: `import DSJSONSchemaValidation`
### Source files
1. Download and copy the repository source files into your project, or add it as a submodule to your git repository.
2. Add the contents of `DSJSONSchemaValidation` directory into your project in Xcode.
3. Import library header: `#import "DSJSONSchema.h"`.
## Usage
After importing the library header/module, use `DSJSONSchema` class to construct schema objects from `NSData` instances:
``` objective-c
NSData *schemaData = [NSData dataWithContentsOfURL:mySchemaURL];
NSError *error = nil;
DSJSONSchema *schema = [DSJSONSchema schemaWithData:schemaData baseURI:nil referenceStorage:nil specification:[DSJSONSchemaSpecification draft4] error:&error];
```
``` swift
if let schemaData = NSData(contentsOfURL: mySchemaURL) {
let schema = try? DSJSONSchema(data: schemaData, baseURI: nil, referenceStorage: nil, specification:DSJSONSchemaSpecification.draft4())
}
```
or from parsed JSON instances:
``` objective-c
NSData *schemaData = [NSData dataWithContentsOfURL:mySchemaURL];
// note that this object might be not an NSDictionary if schema JSON is invalid
NSDictionary *schemaJSON = [NSJSONSerialization JSONObjectWithData:schemaData options:0 error:NULL];
NSError *error = nil;
DSJSONSchema *schema = [DSJSONSchema schemaWithObject:schemaJSON baseURI:nil referenceStorage:nil specification:[DSJSONSchemaSpecification draft4] error:&error];
```
``` swift
if let schemaData = NSData(contentsOfURL: mySchemaURL),
schemaJSON = try? NSJSONSerialization.JSONObjectWithData(schemaData, options: [ ]),
schemaDictionary = schemaJSON as? [String : AnyObject] {
let schema = try? DSJSONSchema(object: schemaDictionary, baseURI: nil, referenceStorage: nil, specification: DSJSONSchemaSpecification.draft4())
}
```
Optional `baseURI` parameter specifies the base scope resolution URI of the constructed schema. Default scope resolution URI is empty.
Optional `referenceStorage` parameter specifies a `DSJSONSchemaStorage` object that should contain "remote" schemas referenced in the instantiated schema. See [Schema storage and external references](#schema-storage-and-external-references) for more details.
After constructing a schema object, you can use it to validate JSON instances. Again, these instances could be provided either as `NSData` objects:
``` objective-c
NSData *jsonData = [NSData dataWithContentsOfURL:myJSONURL];
NSError *validationError = nil;
BOOL success = [schema validateObjectWithData:jsonData error:&validationError];
```
``` swift
if let jsonData = NSData(contentsOfURL: myJSONURL) {
do {
try schema.validateObjectWithData(jsonData)
// Success
} catch let validationError as NSError {
// Failure
}
}
```
or parsed JSON instances:
``` objective-c
NSData *jsonData = [NSData dataWithContentsOfURL:myJSONURL];
id json = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:NULL];
NSError *validationError = nil;
BOOL success = [schema validateObject:json error:&validationError];
```
``` swift
if let jsonData = NSData(contentsOfURL: myJSONURL),
json = try? NSJSONSerialization.JSONObjectWithData(jsonData, options: [ ]) {
do {
try schema.validateObject(json)
// Success
} catch let validationError as NSError {
// Failure
}
}
```
In case of a validation failure, the `NSError` object will contain the