'use strict'; /* * A small JSON-schema subset used for request validation AND emitted verbatim into openapi.json, so there is one * source of truth. Supported: type (string|integer|number|boolean|object|array), enum, minLength, maxLength, pattern, * minimum, maximum, items, maxItems, properties, required. Objects reject unknown keys unless additionalProperties * is true. With coerce: true, strings are turned into numbers/booleans first (query strings). */ const hasOwn = (o, k) => Object.prototype.hasOwnProperty.call(o, k); // JSON.parse makes "__proto__" an OWN key; copying it with plain assignment would swap the prototype of the result. // Reject these keys everywhere, including inside pass-through (additionalProperties) values. const BAD_KEYS = new Set(['__proto__', 'constructor', 'prototype']); function hasBadKey(v, depth) { if (v === null || typeof v !== 'object') return false; if (depth > 8) return true; for (const k of Object.keys(v)) if (BAD_KEYS.has(k) || hasBadKey(v[k], (depth || 0) + 1)) return true; return false; } function check(schema, v, path, errors, coerce) { if (coerce && typeof v === 'string') { if ((schema.type === 'integer' || schema.type === 'number') && v.trim() !== '' && !isNaN(Number(v))) v = Number(v); else if (schema.type === 'boolean' && (v === 'true' || v === 'false')) v = v === 'true'; } const bad = (msg) => { errors.push({ path, message: msg }); return v; }; switch (schema.type) { case 'string': if (typeof v !== 'string') return bad('must be a string'); if (schema.minLength !== undefined && v.length < schema.minLength) return bad('too short (min ' + schema.minLength + ')'); if (schema.maxLength !== undefined && v.length > schema.maxLength) return bad('too long (max ' + schema.maxLength + ')'); if (schema.pattern && !new RegExp(schema.pattern).test(v)) return bad('has the wrong format'); if (schema.enum && !schema.enum.includes(v)) return bad('must be one of ' + schema.enum.join(', ')); return v; case 'integer': case 'number': if (typeof v !== 'number' || !Number.isFinite(v)) return bad('must be a number'); if (schema.type === 'integer' && !Number.isInteger(v)) return bad('must be an integer'); if (schema.minimum !== undefined && v < schema.minimum) return bad('must be >= ' + schema.minimum); if (schema.maximum !== undefined && v > schema.maximum) return bad('must be <= ' + schema.maximum); return v; case 'boolean': if (typeof v !== 'boolean') return bad('must be a boolean'); return v; case 'array': { if (!Array.isArray(v)) return bad('must be an array'); if (schema.maxItems !== undefined && v.length > schema.maxItems) return bad('too many items (max ' + schema.maxItems + ')'); return schema.items ? v.map((it, i) => check(schema.items, it, path + '[' + i + ']', errors, false)) : v; } case 'object': { if (v === null || typeof v !== 'object' || Array.isArray(v)) return bad('must be an object'); const out = {}; const props = schema.properties || {}; for (const k of schema.required || []) if (!hasOwn(v, k) || v[k] === undefined) errors.push({ path: path ? path + '.' + k : k, message: 'is required' }); for (const k of Object.keys(v)) { if (BAD_KEYS.has(k)) { errors.push({ path: path ? path + '.' + k : k, message: 'is not a known field' }); continue; } if (hasOwn(props, k)) out[k] = check(props[k], v[k], path ? path + '.' + k : k, errors, coerce); else if (schema.additionalProperties) { if (hasBadKey(v[k], 0)) errors.push({ path: path ? path + '.' + k : k, message: 'contains a forbidden key' }); else out[k] = v[k]; } else errors.push({ path: path ? path + '.' + k : k, message: 'is not a known field' }); } return out; } default: return v; } } // -> { ok, value, errors: [{ path, message }] } function validate(schema, value, opts) { const errors = []; const out = check(schema, value === undefined ? {} : value, '', errors, !!(opts && opts.coerce)); return { ok: errors.length === 0, value: out, errors }; } module.exports = { validate };