public commit

This commit is contained in:
dswbx
2024-11-16 12:01:47 +01:00
commit 90f80c4280
582 changed files with 49291 additions and 0 deletions

View File

@@ -0,0 +1,120 @@
import { type Static, Type } from "core/utils";
import type { EntityManager } from "data";
import { TransformPersistFailedException } from "../errors";
import { Field, type TActionContext, baseFieldConfigSchema } from "./Field";
export const textFieldConfigSchema = Type.Composite(
[
Type.Object({
default_value: Type.Optional(Type.String()),
minLength: Type.Optional(Type.Number()),
maxLength: Type.Optional(Type.Number()),
pattern: Type.Optional(Type.String()),
html_config: Type.Optional(
Type.Object({
element: Type.Optional(Type.String({ default: "input" })),
props: Type.Optional(
Type.Object(
{},
{
additionalProperties: Type.Union([
Type.String({ title: "String" }),
Type.Number({ title: "Number" })
])
}
)
)
})
)
}),
baseFieldConfigSchema
],
{
additionalProperties: false
}
);
export type TextFieldConfig = Static<typeof textFieldConfigSchema>;
export class TextField<Required extends true | false = false> extends Field<
TextFieldConfig,
string,
Required
> {
override readonly type = "text";
protected getSchema() {
return textFieldConfigSchema;
}
override schema() {
return this.useSchemaHelper("text");
}
override getHtmlConfig() {
if (this.config.html_config) {
return this.config.html_config as any;
}
return super.getHtmlConfig();
}
/**
* Transform value after retrieving from database
* @param value
*/
override transformRetrieve(value: string): string | null {
const val = super.transformRetrieve(value);
// @todo: now sure about these two
if (this.config.maxLength) {
return val.substring(0, this.config.maxLength);
}
if (this.isRequired()) {
return val ? val.toString() : "";
}
return val;
}
override async transformPersist(
_value: any,
em: EntityManager<any>,
context: TActionContext
): Promise<string | undefined> {
let value = await super.transformPersist(_value, em, context);
if (this.nullish(value)) return value;
// transform to string
if (value !== null && typeof value !== "string") {
value = String(value);
}
if (this.config.maxLength && value?.length > this.config.maxLength) {
throw new TransformPersistFailedException(
`Field "${this.name}" must be at most ${this.config.maxLength} character(s)`
);
}
if (this.config.minLength && value?.length < this.config.minLength) {
throw new TransformPersistFailedException(
`Field "${this.name}" must be at least ${this.config.minLength} character(s)`
);
}
return value;
}
override toJsonSchema() {
return this.toSchemaWrapIfRequired(
Type.String({
default: this.getDefault(),
minLength: this.config?.minLength,
maxLength: this.config?.maxLength,
pattern: this.config?.pattern
})
);
}
}