Skip to content

Commit

Permalink
Merge pull request #8688 from amplication/fix/validate-minmax
Browse files Browse the repository at this point in the history
fix(dsg): validate min and max
  • Loading branch information
mulygottlieb committed Jun 20, 2024
2 parents 0f77022 + e553bc1 commit 11b4dac
Show file tree
Hide file tree
Showing 15 changed files with 493 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ export const IS_STRING_ID = builders.identifier("IsString");
export const IS_JSON_VALUE_ID = builders.identifier("IsJSONValue");
export const IS_OPTIONAL_ID = builders.identifier("IsOptional");
export const IS_ENUM_ID = builders.identifier("IsEnum");
export const MIN_ID = builders.identifier("Min");
export const MAX_ID = builders.identifier("Max");
export const MAX_LENGTH_ID = builders.identifier("MaxLength");
export const VALIDATE_NESTED_ID = builders.identifier("ValidateNested");
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import {
IS_JSON_VALUE_ID,
VALIDATE_NESTED_ID,
CLASS_VALIDATOR_CUSTOM_VALIDATORS_MODULE,
MIN_ID,
MAX_ID,
MAX_LENGTH_ID,
} from "./class-validator.util";
import {
CLASS_TRANSFORMER_MODULE,
Expand Down Expand Up @@ -73,6 +76,9 @@ export function getImportableNames(isCustomDto: boolean) {
IS_STRING_ID,
IS_OPTIONAL_ID,
IS_ENUM_ID,
MIN_ID,
MAX_ID,
MAX_LENGTH_ID,
VALIDATE_NESTED_ID,
],
[CLASS_VALIDATOR_CUSTOM_VALIDATORS_MODULE]: [IS_JSON_VALUE_ID],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import { Entity, EntityField } from "@amplication/code-gen-types";
import { classProperty, createGenericArray } from "../../../utils/ast";
import {
isEnumField,
isNumericField,
isOneToOneRelationField,
isTextField,
isToManyRelationField,
} from "../../../utils/field";
import { createPrismaFields } from "../../prisma/create-prisma-schema-fields";
Expand All @@ -25,6 +27,9 @@ import {
IS_NUMBER_ID,
IS_OPTIONAL_ID,
IS_STRING_ID,
MAX_ID,
MAX_LENGTH_ID,
MIN_ID,
VALIDATE_NESTED_ID,
} from "./class-validator.util";
import {
Expand Down Expand Up @@ -156,6 +161,15 @@ export const PARSE_ID = builders.identifier("parse");
export const IS_ARRAY_ID = builders.identifier("isArray");
export const NULLABLE_ID = builders.identifier("nullable");

function createMinMaxDecorator(
identifierName: namedTypes.Identifier,
value: number
): namedTypes.Decorator {
return builders.decorator(
builders.callExpression(identifierName, [builders.numericLiteral(value)])
);
}

/**
*
* create all the body of the classes of the dto like input, object, args, etc...
Expand Down Expand Up @@ -262,6 +276,29 @@ export function createFieldClassProperty(
if (prismaField.type === ScalarType.DateTime && !isQuery) {
decorators.push(createTypeDecorator(DATE_ID));
}
if (!isQuery && isNumericField(field)) {
const minValue = field.properties?.minimumValue;
const maxValue = field.properties?.maximumValue;

if (minValue) {
const minDecorator = createMinMaxDecorator(MIN_ID, minValue);
decorators.push(minDecorator);
}

if (maxValue) {
const maxDecorator = createMinMaxDecorator(MAX_ID, maxValue);
decorators.push(maxDecorator);
}
}
if (!isQuery && isTextField(field)) {
const maxLengthValue = field.properties?.maxLength;
// min length is not exposed in the UI. We have a default value of 1 but we don't want to enforce it

if (maxLengthValue) {
const maxDecorator = createMinMaxDecorator(MAX_LENGTH_ID, maxLengthValue);
decorators.push(maxDecorator);
}
}
if (isEnum) {
const enumId = builders.identifier(createEnumName(field, entity));
apiPropertyDecoratorBuilder.enum(enumId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3447,6 +3447,9 @@ import {
IsOptional,
IsBoolean,
IsNumber,
Min,
Max,
MaxLength,
IsEnum,
ValidateNested,
} from "class-validator";
Expand Down Expand Up @@ -3542,6 +3545,8 @@ class Customer {
type: Number,
})
@IsNumber()
@Min(1)
@Max(100)
@IsOptional()
@Field(() => Float, {
nullable: true,
Expand All @@ -3553,6 +3558,8 @@ class Customer {
type: Number,
})
@IsInt()
@Min(10)
@Max(9007199254740991)
@IsOptional()
@Field(() => GraphQLBigInt, {
nullable: true,
Expand All @@ -3575,6 +3582,7 @@ class Customer {
type: String,
})
@IsString()
@MaxLength(500)
@IsOptional()
@Field(() => String, {
nullable: true,
Expand Down Expand Up @@ -3685,7 +3693,10 @@ import {
IsBoolean,
IsDate,
IsNumber,
Min,
Max,
IsInt,
MaxLength,
IsEnum,
ValidateNested,
} from "class-validator";
Expand Down Expand Up @@ -3757,6 +3768,8 @@ class CustomerCreateInput {
type: Number,
})
@IsNumber()
@Min(1)
@Max(100)
@IsOptional()
@Field(() => Float, {
nullable: true,
Expand All @@ -3768,6 +3781,8 @@ class CustomerCreateInput {
type: Number,
})
@IsInt()
@Min(10)
@Max(9007199254740991)
@IsOptional()
@Field(() => GraphQLBigInt, {
nullable: true,
Expand All @@ -3790,6 +3805,7 @@ class CustomerCreateInput {
type: String,
})
@IsString()
@MaxLength(500)
@IsOptional()
@Field(() => String, {
nullable: true,
Expand Down Expand Up @@ -4231,7 +4247,10 @@ import {
IsBoolean,
IsDate,
IsNumber,
Min,
Max,
IsInt,
MaxLength,
IsEnum,
ValidateNested,
} from "class-validator";
Expand Down Expand Up @@ -4306,6 +4325,8 @@ class CustomerUpdateInput {
type: Number,
})
@IsNumber()
@Min(1)
@Max(100)
@IsOptional()
@Field(() => Float, {
nullable: true,
Expand All @@ -4317,6 +4338,8 @@ class CustomerUpdateInput {
type: Number,
})
@IsInt()
@Min(10)
@Max(9007199254740991)
@IsOptional()
@Field(() => GraphQLBigInt, {
nullable: true,
Expand All @@ -4339,6 +4362,7 @@ class CustomerUpdateInput {
type: String,
})
@IsString()
@MaxLength(500)
@IsOptional()
@Field(() => String, {
nullable: true,
Expand Down Expand Up @@ -12161,8 +12185,10 @@ import { ApiProperty } from "@nestjs/swagger";
import {
IsString,
IsInt,
Max,
IsDate,
IsNumber,
Min,
ValidateNested,
IsOptional,
IsEnum,
Expand Down Expand Up @@ -12232,6 +12258,7 @@ class User {
type: Number,
})
@IsInt()
@Max(120)
@Field(() => Number)
age!: number;

Expand All @@ -12248,6 +12275,8 @@ class User {
type: Number,
})
@IsNumber()
@Min(1)
@Max(999)
@Field(() => Number)
score!: number;

Expand Down Expand Up @@ -12383,8 +12412,10 @@ import { ApiProperty } from "@nestjs/swagger";
import {
IsString,
IsInt,
Max,
IsDate,
IsNumber,
Min,
ValidateNested,
IsOptional,
IsEnum,
Expand Down Expand Up @@ -12456,6 +12487,7 @@ class UserCreateInput {
type: Number,
})
@IsInt()
@Max(120)
@Field(() => Number)
age!: number;

Expand All @@ -12472,6 +12504,8 @@ class UserCreateInput {
type: Number,
})
@IsNumber()
@Min(1)
@Max(999)
@Field(() => Number)
score!: number;

Expand Down Expand Up @@ -12980,8 +13014,10 @@ import {
IsString,
IsOptional,
IsInt,
Max,
IsDate,
IsNumber,
Min,
ValidateNested,
IsEnum,
IsBoolean,
Expand Down Expand Up @@ -13070,6 +13106,7 @@ class UserUpdateInput {
type: Number,
})
@IsInt()
@Max(120)
@IsOptional()
@Field(() => Number, {
nullable: true,
Expand All @@ -13092,6 +13129,8 @@ class UserUpdateInput {
type: Number,
})
@IsNumber()
@Min(1)
@Max(999)
@IsOptional()
@Field(() => Number, {
nullable: true,
Expand Down
Loading

0 comments on commit 11b4dac

Please sign in to comment.