Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: mongoose validation #741

Merged
merged 2 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions api/src/chat/schemas/block.schema.ts
Original file line number Diff line number Diff line change
@@ -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:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
Expand All @@ -9,26 +9,23 @@
import { ModelDefinition, Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Exclude, Transform, Type } from 'class-transformer';
import { Schema as MongooseSchema } from 'mongoose';
import { z } from 'zod';

import { BaseSchema } from '@/utils/generics/base-schema';
import { LifecycleHookManager } from '@/utils/generics/lifecycle-hook-manager';
import { buildZodSchemaValidator } from '@/utils/helpers/zod-validation';
import {
TFilterPopulateFields,
THydratedDocument,
} 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 { Label } from './label.schema';
import { CaptureVar } from './types/capture-var';
import { BlockMessage } from './types/message';
import { CaptureVar, captureVarSchema } from './types/capture-var';
import { BlockMessage, blockMessageObjectSchema } from './types/message';
import { BlockOptions } from './types/options';
import { Pattern } from './types/pattern';
import { Position } from './types/position';
import { Pattern, patternSchema } from './types/pattern';
import { Position, positionSchema } from './types/position';

@Schema({ timestamps: true })
export class BlockStub extends BaseSchema {
Expand All @@ -40,7 +37,7 @@ export class BlockStub extends BaseSchema {

@Prop({
type: Object,
validate: isPatternList,
validate: buildZodSchemaValidator(z.array(patternSchema)),
default: [],
})
patterns: Pattern[];
Expand Down Expand Up @@ -77,7 +74,7 @@ export class BlockStub extends BaseSchema {

@Prop({
type: Object,
validate: isValidMessage,
validate: buildZodSchemaValidator(blockMessageObjectSchema),
})
message: BlockMessage;

Expand Down Expand Up @@ -110,14 +107,14 @@ export class BlockStub extends BaseSchema {

@Prop({
type: Object,
validate: isValidVarCapture,
validate: buildZodSchemaValidator(z.array(captureVarSchema)),
default: [],
})
capture_vars: CaptureVar[];

@Prop({
type: Object,
validate: isPosition,
validate: buildZodSchemaValidator(positionSchema),
})
position: Position;

Expand Down
15 changes: 15 additions & 0 deletions api/src/utils/helpers/zod-validation.ts
Original file line number Diff line number Diff line change
@@ -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).success;
};
}