• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

codekie / openapi-examples-validator / 24230937030

10 Apr 2026 07:03AM UTC coverage: 96.5% (+0.04%) from 96.465%
24230937030

push

github

codekie
chore: npm audit fx

114 of 128 branches covered (89.06%)

386 of 400 relevant lines covered (96.5%)

214.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.76
/src/validator.js
1
/**
2
 * Wrapper for the JSONSchema-validator
3
 */
4

5
/** @import * as ajv from 'ajv-draft-04' */
6

7
const { JSONPath: jsonPath } = require('jsonpath-plus'),
63✔
8
    JsonPointer = require('json-pointer'),
63✔
9
    { default: Ajv } = require('ajv-draft-04'),
63✔
10
    { default: addFormats } = require('ajv-formats');
63✔
11

12
const PROP__ID = '$id',
63✔
13
    JSON_PATH__REFS = '$..\$ref',
63✔
14
    ID__SPEC_SCHEMA = 'https://www.npmjs.com/package/openapi-examples-validator/defs.json',
63✔
15
    ID__RESPONSE_SCHEMA = 'https://www.npmjs.com/package/openapi-examples-validator/schema.json';
63✔
16

17
module.exports = {
63✔
18
    getValidatorFactory,
19
    compileValidate
20
};
21

22
/**
23
 * Get a factory-function to create a prepared validator-instance
24
 * @param {Object}  specSchema  OpenAPI-spec of which potential local references will be extracted
25
 * @param {ajv.Options} [options] Options for the validator
26
 * @returns {function(): Ajv}
27
 */
28
function getValidatorFactory(specSchema, options) {
29
    const preparedSpecSchema = _createReferenceSchema(specSchema);
483✔
30
    return () => {
483✔
31
        const validator = new Ajv(options);
606✔
32
        addFormats(validator);
606✔
33

34
        validator.addSchema(preparedSpecSchema);
606✔
35

36
        return validator;
606✔
37
    };
38
}
39

40
/**
41
 * Compiles the validator-function.
42
 * @param {Ajv}             validator       Validator-instance
43
 * @param {Object}          responseSchema  The response-schema, against the examples will be validated
44
 * @returns {ajv.ValidateFunction}
45
 */
46
function compileValidate(validator, responseSchema) {
47
    const preparedResponseSchema = _prepareResponseSchema(responseSchema, ID__RESPONSE_SCHEMA);
606✔
48
    _replaceRefsToPreparedSpecSchema(preparedResponseSchema);
606✔
49

50
    let result;
51
    try {
606✔
52
        result = validator.compile(preparedResponseSchema);
606✔
53
    } catch (e) {
54
        result = () => {};
×
55
        result.errors = [e];
×
56
    }
57
    return result;
606✔
58
}
59

60
/**
61
 * Prepares the schema, to be used with internal-references
62
 * @param {Object}  specSchema  The schema to be prebared
63
 * @param {String}  idSchema    The unique ID for the schema
64
 * @returns {Object}
65
 * @private
66
 */
67
function _prepareResponseSchema(specSchema, idSchema) {
68
    return { ...specSchema, [PROP__ID]: idSchema };
606✔
69
}
70

71
/**
72
 * Replaces all internal references to the schema, with the extracted references, based on the origin OpenAPI-spec
73
 * @param {Object}  schema  The schema, containing references have to be replaced
74
 * @private
75
 */
76
function _replaceRefsToPreparedSpecSchema(schema) {
77
    jsonPath({
606✔
78
        path: JSON_PATH__REFS,
79
        json: schema,
80
        callback(value, type, payload) {
81
            if (!value.startsWith('#')) { return; }
×
82
            payload.parent[payload.parentProperty] = `${ ID__SPEC_SCHEMA }${ value }`;
×
83
        }
84
    });
85
}
86

87
/**
88
 * Extracts all references and returns a new schema, containing only those.
89
 * @param {Object} specSchema   Schema, which references shall be extracted
90
 * @returns {Object}
91
 * @private
92
 */
93
function _createReferenceSchema(specSchema) {
94
    const refSchema = {
483✔
95
        [PROP__ID]: ID__SPEC_SCHEMA
96
    };
97
    jsonPath({
483✔
98
        path: JSON_PATH__REFS,
99
        json: specSchema,
100
        callback(value) {
101
            if (!value.startsWith('#')) { return; }
×
102
            const pointer = value.substring(1),
×
103
                definition = JsonPointer.get(specSchema, pointer);
×
104
            JsonPointer.set(refSchema, pointer, definition);
×
105
        }
106
    });
107
    return refSchema;
483✔
108
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc