Built: SQLite (node:sqlite) via server/db.js with checksummed migrations, injectable clock, persistent event scheduler with catch-up, tiny REST router (error envelope, idempotency keys, rate limits, bearer tokens with scrypt passwords), lazy-resource economy in public/js/rules.js + data.js, build/cancel/rename commands, seeded map + spawn ring, generated docs/openapi.json. NETWALL net.js/static.js reused. Tested: manual smoke run only (signup, town view, build, map). No unit tests or e2e yet. NOT done: military, expansion, social, client, push, deploy docs, bots/admin. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Q2MJaQ5LXAHzGnTDgceT14
63 lines
3.3 KiB
JavaScript
63 lines
3.3 KiB
JavaScript
'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);
|
|
|
|
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 (hasOwn(props, k)) out[k] = check(props[k], v[k], path ? path + '.' + k : k, errors, coerce);
|
|
else if (schema.additionalProperties) 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 };
|