UNPKG

1.4 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const json_1 = require("@stoplight/json");
4const AJV = require("ajv");
5const jsonSpecv4 = require("ajv/lib/refs/json-schema-draft-04.json");
6const ajv = new AJV({
7 meta: false,
8 schemaId: 'auto',
9 jsonPointers: true,
10});
11ajv.addMetaSchema(jsonSpecv4);
12ajv._opts.defaultMeta = jsonSpecv4.id;
13ajv._refs['http://json-schema.org/schema'] = 'http://json-schema.org/draft-04/schema';
14exports.schema = (targetVal, opts, paths) => {
15 const results = [];
16 const path = paths.target || paths.given;
17 if (!targetVal)
18 return [
19 {
20 path,
21 message: `${paths ? path.join('.') : 'property'} does not exist`,
22 },
23 ];
24 const { schema: schemaObj } = opts;
25 if (!ajv.validate(schemaObj, targetVal) && ajv.errors) {
26 ajv.errors.forEach((e) => {
27 if (e.params && e.params.additionalProperty) {
28 e.message = e.message + ': ' + e.params.additionalProperty;
29 }
30 results.push({
31 path: path.concat(e.dataPath
32 .split('/')
33 .slice(1)
34 .map(frag => json_1.decodePointerFragment(frag))),
35 message: e.message ? e.message : '',
36 });
37 });
38 }
39 return results;
40};
41//# sourceMappingURL=schema.js.map
\No newline at end of file