1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const json_1 = require("@stoplight/json");
|
4 | const AJV = require("ajv");
|
5 | const jsonSpecv4 = require("ajv/lib/refs/json-schema-draft-04.json");
|
6 | const ajv = new AJV({
|
7 | meta: false,
|
8 | schemaId: 'auto',
|
9 | jsonPointers: true,
|
10 | });
|
11 | ajv.addMetaSchema(jsonSpecv4);
|
12 | ajv._opts.defaultMeta = jsonSpecv4.id;
|
13 | ajv._refs['http://json-schema.org/schema'] = 'http://json-schema.org/draft-04/schema';
|
14 | exports.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 |
|
\ | No newline at end of file |