Skip to content

Commit 6551711

Browse files
committed
refactor: change validate logic using Set
1 parent 81fd826 commit 6551711

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

packages/express-cargo/src/validator.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,15 +446,21 @@ export function Without(fieldName: string, message?: cargoErrorMessage): Propert
446446
* @param values - The values that must be present in the array.
447447
* @param message - Optional custom error message.
448448
*/
449-
export function ArrayContains(values: any[], message?: cargoErrorMessage): PropertyDecorator {
449+
export function ArrayContains(values: any[], message?: cargoErrorMessage): TypedPropertyDecorator<any[]> {
450450
return (target: Object, propertyKey: string | symbol): void => {
451451
addValidator(
452452
target,
453453
propertyKey,
454454
new ValidatorRule(
455455
propertyKey,
456456
'arrayContains',
457-
(val: any) => Array.isArray(val) && values.every(v => val.includes(v)),
457+
(value: unknown) => {
458+
if (!Array.isArray(value)) {
459+
return false
460+
}
461+
const valueSet = new Set(value)
462+
return values.every(v => valueSet.has(v))
463+
},
458464
message || `${String(propertyKey)} must contain ${values.join(', ')}`,
459465
),
460466
)

0 commit comments

Comments
 (0)