Skip to content

Commit

Permalink
Use ValidationError instead of Error
Browse files Browse the repository at this point in the history
  • Loading branch information
iankhou committed Jan 16, 2025
1 parent 098a5b2 commit 2d0a107
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/aws-cdk-lib/aws-rds/lib/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,7 @@ abstract class DatabaseClusterNew extends DatabaseClusterBase {
});
}

validateDatabaseClusterProps(props);
validateDatabaseClusterProps(scope, props);

const enablePerformanceInsights = props.enablePerformanceInsights
|| props.performanceInsightRetention !== undefined || props.performanceInsightEncryptionKey !== undefined;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ClusterScailabilityType, DatabaseCluster, DatabaseClusterProps, DBClusterStorageType } from './cluster';
import { PerformanceInsightRetention } from './props';
import { validateProps, ValidationRule } from '../../core/lib/helpers-internal';
import { Construct } from 'constructs';

const standardDatabaseRules: ValidationRule<DatabaseClusterProps>[] = [
{
Expand Down Expand Up @@ -43,11 +44,11 @@ const limitlessDatabaseRules: ValidationRule<DatabaseClusterProps>[] = [
},
];

export function validateDatabaseClusterProps(props: DatabaseClusterProps): void {
export function validateDatabaseClusterProps(scope: Construct, props: DatabaseClusterProps): void {
const isLimitlessCluster = props.clusterScailabilityType === ClusterScailabilityType.LIMITLESS;
const applicableRules = isLimitlessCluster
? [...standardDatabaseRules, ...limitlessDatabaseRules]
: standardDatabaseRules;

validateProps(DatabaseCluster.name, props, applicableRules);
validateProps(scope, DatabaseCluster.name, props, applicableRules);
}
2 changes: 1 addition & 1 deletion packages/aws-cdk-lib/aws-sqs/lib/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ export class Queue extends QueueBase {
physicalName: props.queueName,
});

validateQueueProps(props);
validateQueueProps(scope, props);

if (props.redriveAllowPolicy) {
const { redrivePermission, sourceQueues } = props.redriveAllowPolicy;
Expand Down
5 changes: 3 additions & 2 deletions packages/aws-cdk-lib/aws-sqs/lib/validate-queue-props.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Queue, QueueProps } from './index';
import { Token } from '../../core';
import { validateProps, ValidationRule } from '../../core/lib/helpers-internal';
import { Construct } from 'constructs';

function validateRange(value: number | undefined, minValue: number, maxValue: number): boolean {
return value !== undefined && !Token.isUnresolved(value) && (value < minValue || value > maxValue);
Expand Down Expand Up @@ -33,6 +34,6 @@ const queueValidationRules: ValidationRule<QueueProps>[] = [
},
];

export function validateQueueProps(props: QueueProps) {
validateProps(Queue.name, props, queueValidationRules);
export function validateQueueProps(scope: Construct, props: QueueProps) {
validateProps(scope, Queue.name, props, queueValidationRules);
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { Construct } from 'constructs';
import { ValidationError } from '../errors';

/**
* Represents a validation rule for props of type T.
* @template T The type of the props being validated.
Expand Down Expand Up @@ -27,13 +30,13 @@ export type ValidationRule<T> = {
* @param {ValidationRule<T>[]} rules - An array of validation rules to apply.
* @throws {Error} If any validation rules fail, with a message detailing all failures.
*/
export function validateProps<T>(className: string, props: T, rules: ValidationRule<T>[]): void {
export function validateProps<T>(scope: Construct, className: string, props: T, rules: ValidationRule<T>[]): void {
const validationErrors = rules
.filter(rule => rule.condition(props))
.map(rule => rule.message(props));

if (validationErrors.length > 0) {
const errorMessage = `${className} initialization failed due to the following validation error(s):\n${validationErrors.map(error => `- ${error}`).join('\n')}`;
throw new Error(errorMessage);
throw new ValidationError(errorMessage, scope);
}
}

0 comments on commit 2d0a107

Please sign in to comment.