API reference › @evolu/common › Type › maxEntries

function maxEntries<Max extends number>(
  max: ValidateBrandFactoryCount<Max>,
): <ParentType>(
  parent: EntriesParent<`MaxEntries${Max}`, ParentType>,
) => BrandType<ParentType, `MaxEntries${Max}`, MaxEntriesError<Max>>;

Defined in: packages/common/src/Type.ts:16490

Maximum entry count Brand for record and object Outputs with at most max entries.

Counts entries like minEntries. The count is checked after the parent has validated every entry, so it does not limit the validation work for a large input.

Example

import {
  assertEqual,
  assertErr,
  assertOk,
  maxEntries,
  record,
  String,
} from "@evolu/common";

const Labels = maxEntries(2)(record(String, String));

assertOk(Labels.fromUnknown({ en: "Hello", cs: "Ahoj" }), {
  en: "Hello",
  cs: "Ahoj",
});

const value = { en: "Hello", cs: "Ahoj", de: "Hallo" };
const invalid = Labels.fromUnknown(value);
assertErr(invalid, { type: "MaxEntries2", value, max: 2 });
assertEqual(
  Labels.formatError(invalid.error),
  'The value {"en":"Hello","cs":"Ahoj","de":"Hallo"} exceeds the maximum entry count of 2.',
);