/
githubmirror
/
novu
Обзор
Документация
Войти
/
githubmirror
/
novu
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
next
apps/api/src/app/shared/validators/json-schema.validator.ts
39 строк
1 KB
Adam Chmara
chore(root): apply biome formatter & linter fixes NV-6436 (#8838)
31 июл 2025, 14:59
Не верифицирован
31 июл 2025, 14:59
855fb8f
Код
Авторство
О чём код?
import Ajv from 'ajv'; import addFormats from 'ajv-formats'; import { registerDecorator, ValidationArguments, ValidationOptions } from 'class-validator'; export function IsValidJsonSchema(validationOptions?: ValidationOptions & { nullable?: boolean }) { return (object: object, propertyName: string) => { registerDecorator({ name: 'isValidJsonSchema', target: object.constructor, propertyName, options: validationOptions, validator: { validate(value: any, args: ValidationArguments) { if (!value || typeof value !== 'object') { if (validationOptions?.nullable && !value) { return true; } return false; } try { const ajv = new Ajv({ strict: false }); addFormats(ajv); ajv.compile(value); return true; } catch (error) { return false; } }, defaultMessage(args: ValidationArguments) { return `${args.property} must be a valid JSON schema`; }, }, }); }; }