Files
outline/server/models/validators/TextLength.ts
T
Tom Moor 5610df5a26 chore: Reduce no-explicit-any warnings in server directory (#12202)
* chore: Reduce no-explicit-any warnings in server directory

Tightens types across test response bodies, decorator signatures, the
TestServer wrapper, base class generics, and presenter Record types.
Where any is genuinely load-bearing (Sequelize model generics,
PropertyDescriptor decorator returns, plugin-registered template
classes, Fix mixin), keeps any with a targeted eslint-disable plus
reason rather than masking the constraint. Cuts server-only
no-explicit-any warnings from 162 to 70.

* fix: groups test asserts on first response instead of second

Caught by Copilot review on the no-explicit-any cleanup. Also fixes
the pre-existing getChangsetSkipped → getChangesetSkipped typo
surfaced while reviewing nearby decorator code.
2026-04-28 19:50:45 -04:00

40 lines
1.1 KiB
TypeScript

import size from "lodash/size";
import { Node } from "prosemirror-model";
import { addAttributeOptions } from "sequelize-typescript";
import type { ProsemirrorData } from "@shared/types";
import { ProsemirrorHelper } from "@shared/utils/ProsemirrorHelper";
import { schema } from "@server/editor";
/**
* A decorator that validates the size of the text within a prosemirror data
* object, taking into account unicode characters of variable lengths.
*/
export default function TextLength({
msg,
min = 0,
max,
}: {
msg?: string;
min?: number;
max: number;
}): (target: object, propertyName: string) => void {
return (target: object, propertyName: string) =>
addAttributeOptions(target, propertyName, {
validate: {
validLength(value: ProsemirrorData) {
let text;
try {
text = ProsemirrorHelper.toPlainText(Node.fromJSON(schema, value));
} catch (_err) {
throw new Error("Invalid data");
}
if (size(text) > max || size(text) < min) {
throw new Error(msg);
}
},
},
});
}