Skip to content

Commit 8e34fbe

Browse files
authored
fix: correct Without validator failing when instance is absent (#241)
1 parent 9bd8261 commit 8e34fbe

2 files changed

Lines changed: 4 additions & 6 deletions

File tree

‎packages/express-cargo/src/decorators/validators/crossField.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,8 @@ export function Without(fieldName: string, message?: cargoErrorMessage): Propert
3535
new ValidatorRule(
3636
propertyKey,
3737
'without',
38-
(value: unknown, instance?: Record<string | symbol, any>) => {
39-
if (!instance) return false
40-
return !(!!value && !!instance?.[fieldName])
41-
},
38+
(value: unknown, instance?: Record<string | symbol, any>) =>
39+
!(!!value && !!instance?.[fieldName]),
4240
message || `${String(propertyKey)} cannot exist with ${fieldName}`,
4341
),
4442
{ name: Without.name, category: 'validator', args: [fieldName] },

‎packages/express-cargo/tests/validator/without.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ describe('Without decorator', () => {
7474
expect(result).toBeNull()
7575
})
7676

77-
it('Case 5: Should pass when instance is undefined', () => {
77+
it('Case 5: Should pass when instance is undefined (related field is absent)', () => {
7878
const meta = classMeta.getFieldMetadata(TARGET_FIELD)
7979
const WithoutRule = meta.getValidators()?.find(v => v.type === 'without')
8080
const instance = undefined
8181
const result = WithoutRule?.validate('ValueA', instance)
8282

83-
expect(result).toBeInstanceOf(CargoFieldError)
83+
expect(result).toBeNull()
8484
})
8585
})

0 commit comments

Comments
 (0)