Skip to content

Commit

Permalink
feat: add validators
Browse files Browse the repository at this point in the history
  • Loading branch information
johannesschobel committed Feb 21, 2020
1 parent edc9d2d commit 9620cf1
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 5 deletions.
6 changes: 5 additions & 1 deletion packages/query-graphql/src/types/create-many-args.type.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Class } from '@nestjs-query/core';
import { Field, ArgsType } from 'type-graphql';
import { Type } from 'class-transformer';
import { ValidateNested } from 'class-validator';
import { ArgsType, Field } from 'type-graphql';

export interface CreateManyArgsType<C> {
input: C[];
Expand All @@ -8,6 +10,8 @@ export interface CreateManyArgsType<C> {
export function CreateManyArgsType<C>(ItemClass: Class<C>): Class<CreateManyArgsType<C>> {
@ArgsType()
class CreateManyArgs implements CreateManyArgsType<C> {
@Type(() => ItemClass)
@ValidateNested({ each: true })
@Field(() => [ItemClass], { description: 'Array of records to create' })
input!: C[];
}
Expand Down
6 changes: 5 additions & 1 deletion packages/query-graphql/src/types/create-one-args.type.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Class } from '@nestjs-query/core';
import { Field, ArgsType } from 'type-graphql';
import { Type } from 'class-transformer';
import { ValidateNested } from 'class-validator';
import { ArgsType, Field } from 'type-graphql';

export interface CreateOneArgsType<C> {
input: C;
Expand All @@ -8,6 +10,8 @@ export interface CreateOneArgsType<C> {
export function CreateOneArgsType<C>(ItemClass: Class<C>): Class<CreateOneArgsType<C>> {
@ArgsType()
class CreateOneArgs implements CreateOneArgsType<C> {
@Type(() => ItemClass)
@ValidateNested()
@Field(() => ItemClass, { description: 'The record to create' })
input!: C;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/query-graphql/src/types/delete-many-args.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export interface DeleteManyArgsType<T> {
export function DeleteManyArgsType<T>(FilterType: Class<Filter<T>>): Class<DeleteManyArgsType<T>> {
@ArgsType()
class DeleteManyArgs implements DeleteManyArgsType<T> {
@Field(() => FilterType, { description: 'Filter to find records to delete' })
@IsNotEmptyObject()
@Field(() => FilterType, { description: 'Filter to find records to delete' })
input!: Filter<T>;
}
return DeleteManyArgs;
Expand Down
2 changes: 2 additions & 0 deletions packages/query-graphql/src/types/delete-one-args.type.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Class } from '@nestjs-query/core';
import { Field, ID, ArgsType } from 'type-graphql';
import { IsNotEmpty } from 'class-validator';

export interface DeleteOneArgsType {
id: string | number;
Expand All @@ -13,6 +14,7 @@ export function DeleteOneArgsType(): Class<DeleteOneArgsType> {
}
@ArgsType()
class DeleteOneArgs implements DeleteOneArgsType {
@IsNotEmpty()
@Field(() => ID, { description: 'The id of the record to delete.' })
id!: string | number;
}
Expand Down
7 changes: 5 additions & 2 deletions packages/query-graphql/src/types/update-many-args.type.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { DeepPartial, Filter, Class } from '@nestjs-query/core';
import { Field, ArgsType } from 'type-graphql';
import { IsNotEmptyObject } from 'class-validator';
import { IsNotEmptyObject, ValidateNested } from 'class-validator';
import { Type } from 'class-transformer';

export interface UpdateManyArgsType<T, U extends DeepPartial<T>> {
filter: Filter<T>;
Expand All @@ -13,10 +14,12 @@ export function UpdateManyArgsType<T, U extends DeepPartial<T>>(
): Class<UpdateManyArgsType<T, U>> {
@ArgsType()
class UpdateManyArgs implements UpdateManyArgsType<T, U> {
@Field(() => FilterType, { description: 'Filter used to find fields to update' })
@IsNotEmptyObject()
@Field(() => FilterType, { description: 'Filter used to find fields to update' })
filter!: Filter<T>;

@Type(() => UpdateType)
@ValidateNested()
@Field(() => UpdateType, { description: 'The update to apply to all records found using the filter' })
input!: U;
}
Expand Down
5 changes: 5 additions & 0 deletions packages/query-graphql/src/types/update-one-args.type.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { DeepPartial, Class } from '@nestjs-query/core';
import { Field, ID, ArgsType } from 'type-graphql';
import { IsNotEmpty, ValidateNested } from 'class-validator';
import { Type } from 'class-transformer';

export interface UpdateOneArgsType<DTO, U extends DeepPartial<DTO>> {
id: string | number;
Expand All @@ -9,9 +11,12 @@ export interface UpdateOneArgsType<DTO, U extends DeepPartial<DTO>> {
export function UpdateOneArgsType<T, U extends DeepPartial<T>>(UpdateType: Class<U>): Class<UpdateOneArgsType<T, U>> {
@ArgsType()
class UpdateOneArgs implements UpdateOneArgsType<T, U> {
@IsNotEmpty()
@Field(() => ID, { description: 'The id of the record to update' })
id!: string | number;

@Type(() => UpdateType)
@ValidateNested()
@Field(() => UpdateType, { description: 'The update to apply.' })
input!: U;
}
Expand Down

0 comments on commit 9620cf1

Please sign in to comment.