mirror of
https://github.com/hexastack/hexabot
synced 2025-02-22 20:38:32 +00:00
feat: refactor mongoose validation with higher order function
This commit is contained in:
parent
457d49ca27
commit
4d549b8710
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright © 2024 Hexastack. All rights reserved.
|
* Copyright © 2025 Hexastack. All rights reserved.
|
||||||
*
|
*
|
||||||
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
|
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
|
||||||
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
|
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
|
||||||
@ -9,26 +9,23 @@
|
|||||||
import { ModelDefinition, Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
|
import { ModelDefinition, Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
|
||||||
import { Exclude, Transform, Type } from 'class-transformer';
|
import { Exclude, Transform, Type } from 'class-transformer';
|
||||||
import { Schema as MongooseSchema } from 'mongoose';
|
import { Schema as MongooseSchema } from 'mongoose';
|
||||||
|
import { z } from 'zod';
|
||||||
|
|
||||||
import { BaseSchema } from '@/utils/generics/base-schema';
|
import { BaseSchema } from '@/utils/generics/base-schema';
|
||||||
import { LifecycleHookManager } from '@/utils/generics/lifecycle-hook-manager';
|
import { LifecycleHookManager } from '@/utils/generics/lifecycle-hook-manager';
|
||||||
|
import { buildZodSchemaValidator } from '@/utils/helpers/zod-validation';
|
||||||
import {
|
import {
|
||||||
TFilterPopulateFields,
|
TFilterPopulateFields,
|
||||||
THydratedDocument,
|
THydratedDocument,
|
||||||
} from '@/utils/types/filter.types';
|
} from '@/utils/types/filter.types';
|
||||||
|
|
||||||
import { isValidMessage } from '../validation-rules/is-message';
|
|
||||||
import { isPatternList } from '../validation-rules/is-pattern-list';
|
|
||||||
import { isPosition } from '../validation-rules/is-position';
|
|
||||||
import { isValidVarCapture } from '../validation-rules/is-valid-capture';
|
|
||||||
|
|
||||||
import { Category } from './category.schema';
|
import { Category } from './category.schema';
|
||||||
import { Label } from './label.schema';
|
import { Label } from './label.schema';
|
||||||
import { CaptureVar } from './types/capture-var';
|
import { CaptureVar, captureVarSchema } from './types/capture-var';
|
||||||
import { BlockMessage } from './types/message';
|
import { BlockMessage, blockMessageObjectSchema } from './types/message';
|
||||||
import { BlockOptions } from './types/options';
|
import { BlockOptions } from './types/options';
|
||||||
import { Pattern } from './types/pattern';
|
import { Pattern, patternSchema } from './types/pattern';
|
||||||
import { Position } from './types/position';
|
import { Position, positionSchema } from './types/position';
|
||||||
|
|
||||||
@Schema({ timestamps: true })
|
@Schema({ timestamps: true })
|
||||||
export class BlockStub extends BaseSchema {
|
export class BlockStub extends BaseSchema {
|
||||||
@ -40,7 +37,7 @@ export class BlockStub extends BaseSchema {
|
|||||||
|
|
||||||
@Prop({
|
@Prop({
|
||||||
type: Object,
|
type: Object,
|
||||||
validate: isPatternList,
|
validate: buildZodSchemaValidator(z.array(patternSchema)),
|
||||||
default: [],
|
default: [],
|
||||||
})
|
})
|
||||||
patterns: Pattern[];
|
patterns: Pattern[];
|
||||||
@ -77,7 +74,7 @@ export class BlockStub extends BaseSchema {
|
|||||||
|
|
||||||
@Prop({
|
@Prop({
|
||||||
type: Object,
|
type: Object,
|
||||||
validate: isValidMessage,
|
validate: buildZodSchemaValidator(blockMessageObjectSchema),
|
||||||
})
|
})
|
||||||
message: BlockMessage;
|
message: BlockMessage;
|
||||||
|
|
||||||
@ -110,14 +107,14 @@ export class BlockStub extends BaseSchema {
|
|||||||
|
|
||||||
@Prop({
|
@Prop({
|
||||||
type: Object,
|
type: Object,
|
||||||
validate: isValidVarCapture,
|
validate: buildZodSchemaValidator(z.array(captureVarSchema)),
|
||||||
default: [],
|
default: [],
|
||||||
})
|
})
|
||||||
capture_vars: CaptureVar[];
|
capture_vars: CaptureVar[];
|
||||||
|
|
||||||
@Prop({
|
@Prop({
|
||||||
type: Object,
|
type: Object,
|
||||||
validate: isPosition,
|
validate: buildZodSchemaValidator(positionSchema),
|
||||||
})
|
})
|
||||||
position: Position;
|
position: Position;
|
||||||
|
|
||||||
|
15
api/src/utils/helpers/zod-validation.ts
Normal file
15
api/src/utils/helpers/zod-validation.ts
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
/*
|
||||||
|
* Copyright © 2025 Hexastack. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
|
||||||
|
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
|
||||||
|
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { ZodSchema } from 'zod';
|
||||||
|
|
||||||
|
export function buildZodSchemaValidator<T>(zodSchema: ZodSchema<T>) {
|
||||||
|
return (data: unknown) => {
|
||||||
|
return zodSchema.safeParse(data);
|
||||||
|
};
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user