fix: forbid creation of objects or fields with certain characters or with forbidden keywords that clashes with pg_graphql (#3957)
* fix: forbid creation of objects or fields with certain characters or with forbidden keywords that clashes with pg_graphql * refactor: add a decorator for name validation and use it on fields
This commit is contained in:
committed by
GitHub
parent
b1eb0577bc
commit
0fe838d320
@ -0,0 +1,24 @@
|
|||||||
|
import {
|
||||||
|
registerDecorator,
|
||||||
|
ValidationOptions,
|
||||||
|
ValidationArguments,
|
||||||
|
} from 'class-validator';
|
||||||
|
|
||||||
|
export function IsValidName(validationOptions?: ValidationOptions) {
|
||||||
|
return function (object: object, propertyName: string) {
|
||||||
|
registerDecorator({
|
||||||
|
name: 'IsValidName',
|
||||||
|
target: object.constructor,
|
||||||
|
propertyName: propertyName,
|
||||||
|
options: validationOptions,
|
||||||
|
validator: {
|
||||||
|
validate(value: any) {
|
||||||
|
return /^(?!(?:not|or|and)$)[^'\"\\;.=*/]+$/.test(value);
|
||||||
|
},
|
||||||
|
defaultMessage(args: ValidationArguments) {
|
||||||
|
return `${args.property} has failed the name validation check`;
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
|
}
|
||||||
@ -34,6 +34,7 @@ import { FieldMetadataType } from 'src/metadata/field-metadata/field-metadata.en
|
|||||||
import { BeforeDeleteOneField } from 'src/metadata/field-metadata/hooks/before-delete-one-field.hook';
|
import { BeforeDeleteOneField } from 'src/metadata/field-metadata/hooks/before-delete-one-field.hook';
|
||||||
import { IsFieldMetadataDefaultValue } from 'src/metadata/field-metadata/validators/is-field-metadata-default-value.validator';
|
import { IsFieldMetadataDefaultValue } from 'src/metadata/field-metadata/validators/is-field-metadata-default-value.validator';
|
||||||
import { IsFieldMetadataOptions } from 'src/metadata/field-metadata/validators/is-field-metadata-options.validator';
|
import { IsFieldMetadataOptions } from 'src/metadata/field-metadata/validators/is-field-metadata-options.validator';
|
||||||
|
import { IsValidName } from 'src/metadata/decorators/is-valid-name.decorator';
|
||||||
|
|
||||||
registerEnumType(FieldMetadataType, {
|
registerEnumType(FieldMetadataType, {
|
||||||
name: 'FieldMetadataType',
|
name: 'FieldMetadataType',
|
||||||
@ -74,6 +75,7 @@ export class FieldMetadataDTO<
|
|||||||
@IsString()
|
@IsString()
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
@Field()
|
@Field()
|
||||||
|
@IsValidName()
|
||||||
name: string;
|
name: string;
|
||||||
|
|
||||||
@IsString()
|
@IsString()
|
||||||
|
|||||||
@ -3,6 +3,7 @@ import { Field, HideField, InputType } from '@nestjs/graphql';
|
|||||||
import { BeforeCreateOne } from '@ptc-org/nestjs-query-graphql';
|
import { BeforeCreateOne } from '@ptc-org/nestjs-query-graphql';
|
||||||
import { IsNotEmpty, IsOptional, IsString, IsUUID } from 'class-validator';
|
import { IsNotEmpty, IsOptional, IsString, IsUUID } from 'class-validator';
|
||||||
|
|
||||||
|
import { IsValidName } from 'src/metadata/decorators/is-valid-name.decorator';
|
||||||
import { BeforeCreateOneObject } from 'src/metadata/object-metadata/hooks/before-create-one-object.hook';
|
import { BeforeCreateOneObject } from 'src/metadata/object-metadata/hooks/before-create-one-object.hook';
|
||||||
|
|
||||||
@InputType()
|
@InputType()
|
||||||
@ -11,11 +12,13 @@ export class CreateObjectInput {
|
|||||||
@IsString()
|
@IsString()
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
@Field()
|
@Field()
|
||||||
|
@IsValidName()
|
||||||
nameSingular: string;
|
nameSingular: string;
|
||||||
|
|
||||||
@IsString()
|
@IsString()
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
@Field()
|
@Field()
|
||||||
|
@IsValidName()
|
||||||
namePlural: string;
|
namePlural: string;
|
||||||
|
|
||||||
@IsString()
|
@IsString()
|
||||||
|
|||||||
@ -3,6 +3,7 @@ import { Field, InputType } from '@nestjs/graphql';
|
|||||||
import { BeforeUpdateOne } from '@ptc-org/nestjs-query-graphql';
|
import { BeforeUpdateOne } from '@ptc-org/nestjs-query-graphql';
|
||||||
import { IsBoolean, IsOptional, IsString, IsUUID } from 'class-validator';
|
import { IsBoolean, IsOptional, IsString, IsUUID } from 'class-validator';
|
||||||
|
|
||||||
|
import { IsValidName } from 'src/metadata/decorators/is-valid-name.decorator';
|
||||||
import { BeforeUpdateOneObject } from 'src/metadata/object-metadata/hooks/before-update-one-object.hook';
|
import { BeforeUpdateOneObject } from 'src/metadata/object-metadata/hooks/before-update-one-object.hook';
|
||||||
|
|
||||||
@InputType()
|
@InputType()
|
||||||
@ -21,11 +22,13 @@ export class UpdateObjectInput {
|
|||||||
@IsString()
|
@IsString()
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
@Field({ nullable: true })
|
@Field({ nullable: true })
|
||||||
|
@IsValidName()
|
||||||
nameSingular?: string;
|
nameSingular?: string;
|
||||||
|
|
||||||
@IsString()
|
@IsString()
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
@Field({ nullable: true })
|
@Field({ nullable: true })
|
||||||
|
@IsValidName()
|
||||||
namePlural?: string;
|
namePlural?: string;
|
||||||
|
|
||||||
@IsString()
|
@IsString()
|
||||||
|
|||||||
Reference in New Issue
Block a user