electrodb icon indicating copy to clipboard operation
electrodb copied to clipboard

Docs inconsistency: attributes.validate boolean semantics reversed vs v3 migration (true should be valid)

Open shinking02 opened this issue 3 months ago • 2 comments

Summary

The boolean semantics for attributes.validate in the docs appear inverted for v3.

  • The Attributes page says: (value: T) => booleantrue (or truthy) signifies invalid, false (or falsy) is considered valid.
  • The v3 Migration guide says the opposite and also states the callback return type is strictly boolean: return true for valid values and false for invalid values.

Observed runtime behavior in v3 matches the migration guide (true = valid, false = invalid).

A related discussion from before v3 also suggested flipping semantics and treating this as a breaking change: https://github.com/tywalch/electrodb/discussions/411

Environment

  • ElectroDB: v3.x (observed on v3.4.5)
  • Node.js: 24.4.1

Where the docs disagree

  • Attributes → Attribute Validation: shows true = invalid, false = valid and also lists legacy signatures (RegExp, (value) => string, (value) => void).
    https://electrodb.dev/en/modeling/attributes/#attribute-validation

  • v3 Migration → Changes to validate callback on attributes: “strict return type of boolean” and “return true for valid, false for invalid”.
    https://electrodb.dev/en/core-concepts/v3-migration/#changes-to-validate-callback-on-attributes

Minimal reproduction

import { Entity } from "electrodb";

const table = "test";

const Passes = new Entity({
  model: { entity: "e", service: "svc", version: "1" },
  attributes: {
    id: { type: "string", required: true },
    v:  { type: "string", required: true, validate: () => true }, // <-- true should ACCEPT
  },
  indexes: { primary: { pk: { field: "pk", composite: ["id"] }, sk: { field: "sk", composite: [] } } }
}, { table });

await Passes.create({ id: "1", v: "x" }).go(); // ✅ passes

const Fails = new Entity({
  model: { entity: "e2", service: "svc", version: "1" },
  attributes: {
    id: { type: "string", required: true },
    v:  { type: "string", required: true, validate: () => false }, // <-- false should REJECT
  },
  indexes: { primary: { pk: { field: "pk", composite: ["id"] }, sk: { field: "sk", composite: [] } } }
}, { table });

await Fails.create({ id: "1", v: "x" }).go(); // ❌ throws ElectroValidationError: Invalid Attribute (code 3001)

shinking02 avatar Oct 09 '25 10:10 shinking02

The documentation (here: https://electrodb.dev/en/modeling/attributes/#attribute-validation) also describes several different function signatures, but only the one that returns boolean is valid and (as @shinking02 points out) its sense is reversed.

Docs need to be updated.

natesilva avatar Nov 25 '25 21:11 natesilva

I aim to create a PR for this issue within the year.

shinking02 avatar Nov 26 '25 23:11 shinking02