Adding example to load official draft-04 to README

master
xdu 2018-12-14 23:46:47 -08:00 committed by GitHub
parent 1de2a5f5f2
commit 3df9b7302f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 0 deletions

View File

@ -34,6 +34,21 @@ var ajv = new Ajv({schemaId: 'id'});
ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-04.json'));
```
In case the above meta schema gives you validation error on `id` or `$schema` see [issue](https://github.com/epoberezkin/ajv/issues/904) you can load the official meta schema json instead. Example with `axios` (feel free to replace `axios` with the your preferred http request package):
```javascript
// inside an async function.
const axios = require('axios');
const util = require('util');
const response = await axios.get('http://json-schema.org/draft-04/schema#');
if (response.status != 200) {
throw new Error(util.format('could not load draft-04 schema. status=%s', response.status));
}
ajv.addMetaSchema(response.data);
```
## Contents