Security

Request validation

A trust boundary is a point where data enters a component from a less-trusted source. Validate the shape, size and meaning of request data before business logic uses it. Validation complements authentication, authorization and safe storage operations; it does not replace them. Never spread an entire request body into a persistence model. TypeScript types do not validate incoming JSON at runtime. Reject oversized bodies while reading the stream in a real server, not only after collecting them. Evidence: local teaching fixture, not a production security guarantee. Source: https://cheatsheetseries.owasp.org/cheatsheets/REST_Security_Cheat_Sheet.html

Formal Syntax & Structure

nodejs syntaxnodejs
parseItem(value) → validated object or null (lesson-defined contract)

Working Example

Example Snippetnodejs
function parseItem(value) {  if (!value || typeof value !== 'object' || Array.isArray(value)) return null;  const keys = Object.keys(value);  if (keys.length !== 2 || !keys.includes('sku') || !keys.includes('quantity')) return null;  if (typeof value.sku !== 'string' || !/^[A-Z0-9]{1,12}$/.test(value.sku)) return null;  if (!Number.isInteger(value.quantity) || value.quantity < 1 || value.quantity > 100) return null;  return {sku:value.sku,quantity:value.quantity};}for (const v of [{sku:'B1',quantity:2},{sku:'B1',quantity:'2'}, {sku:'B1',quantity:2,owner:'u2'},{sku:'B1',quantity:101}]) {  console.log(JSON.stringify(parseItem(v)));}
Node.js Reference