All files / app validator.js

44.77% Statements 30/67
11.62% Branches 5/43
50% Functions 6/12
46.15% Lines 30/65

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 1461x 1x 1x 1x   1x 14x   14x 30x   6x       6x   6x 10x 10x   6x                     18x       14x                 14x     1x 4x 4x 4x 4x 20x 20x 20x   16x   20x     1x             1x                                                         4x                                                                                              
const Ajv = require('./ajv');
const addFormats = require('ajv-formats');
const ajv = new Ajv({allErrors: true, $data: true, useDefaults: true, strict: false});
addFormats(ajv);
 
const decorateSchema = originalSchema => {
    const schema = {...originalSchema};
 
    Object.keys(schema).forEach(key => {
        switch (key) {
            case 'required':
                Iif (typeof schema.required === 'boolean') {
                    schema['x-required'] = schema.required; // json schema 4 support
                    delete schema.required;
                }
                break;
            case 'properties':
                schema.properties = Object.entries(schema.properties).reduce((all, [key, value]) => {
                    all[key] = decorateSchema(value);
                    return all;
                }, {});
                break;
            case 'items':
            case 'not':
                schema[key] = decorateSchema(schema[key]);
                break;
            case 'oneOf':
            case 'allOf':
            case 'anyOf':
                schema[key] = schema[key].map(decorateSchema);
                break;
            default:
                break;
        }
    });
 
    Iif (schema['x-nullable'] === true) {
        return {
            oneOf: [
                schema,
                { type: 'null' }
            ]
        };
    }
 
    return schema;
};
 
const getValidationHandler = originalSchema => {
    const schema = decorateSchema(originalSchema);
    schema.$async = true;
    const validate = ajv.compile(schema);
    return async value => {
        const validation = {result: value};
        try {
            await validate(value);
        } catch (e) {
            validation.error = e;
        }
        return validation;
    };
};
const collectionFormats = {
    csv: ',',
    ssv: ' ',
    tsv: '\t',
    pipes: '|'
};
 
module.exports = {
    empty: () => {
        return getValidationHandler({
            oneOf: [
                {
                    type: 'null'
                },
                {
                    type: 'string',
                    maxLength: 0
                },
                {
                    type: 'object',
                    additionalProperties: false,
                    properties: {}
                }
            ]
        });
    },
    file: schema => {
        return getValidationHandler({
            in: schema.in,
            name: schema.name,
            description: schema.description,
            required: schema.required,
            'x-file': true
        });
    },
    json: schema => {
        return getValidationHandler(schema);
    },
    primitive: schema => {
        const validate = getValidationHandler(schema);
        return value => { // normalize value
            if (typeof value !== 'undefined') {
                switch (schema.type) {
                    case 'number':
                    case 'integer':
                        if (!isNaN(value)) {
                            value = +value;
                        }
                        break;
                    case 'boolean':
                        if (value === 'true') {
                            value = true;
                        } else if (value === 'false') {
                            value = false;
                        }
                        break;
                    case 'array':
                        if (!Array.isArray(value)) {
                            const format = collectionFormats[schema.collectionFormat || 'csv'];
                            value = format ? String(value).split(format) : [value];
                        }
                        switch (schema.items.type) {
                            case 'number':
                            case 'integer':
                                value = value.map(v => isNaN(v) ? v : +v);
                                break;
                            case 'boolean':
                                value = value.map(v => v === 'true' ? true : v === 'false' ? false : v);
                                break;
                            default:
                                break;
                        }
                        break;
                    default:
                        break;
                }
            } else if (!schema.required) {
                return { result: value };
            }
            return validate(value);
        };
    }
};