Skip to content

Commit

Permalink
fix: use the mutated value for validating minLength and maxLength
Browse files Browse the repository at this point in the history
Closes: #147
  • Loading branch information
thetutlage committed May 17, 2022
1 parent 2d63698 commit 80081bb
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 46 deletions.
5 changes: 2 additions & 3 deletions src/Validations/string-and-array/maxLength.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@ export const maxLength: SyncValidation<{ maxLength: number; subtype: NodeSubType
},
}
}),
validate(value, compiledOptions, { tip, field, errorReporter, pointer, arrayExpressionPointer }) {
validate(value, compiledOptions, { errorReporter, pointer, arrayExpressionPointer }) {
if (compiledOptions.subtype === 'array' && !Array.isArray(value)) {
return
} else if (compiledOptions.subtype === 'string' && typeof value !== 'string') {
return
}

const originalValue = compiledOptions.subtype === 'string' ? tip[field] || value : value
if (originalValue.length > compiledOptions.maxLength) {
if (value.length > compiledOptions.maxLength) {
errorReporter.report(pointer, RULE_NAME, DEFAULT_MESSAGE, arrayExpressionPointer, {
maxLength: compiledOptions.maxLength,
})
Expand Down
5 changes: 2 additions & 3 deletions src/Validations/string-and-array/minLength.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@ export const minLength: SyncValidation<{ minLength: number; subtype: NodeSubType
},
}
}),
validate(value, compiledOptions, { field, tip, errorReporter, pointer, arrayExpressionPointer }) {
validate(value, compiledOptions, { errorReporter, pointer, arrayExpressionPointer }) {
if (compiledOptions.subtype === 'array' && !Array.isArray(value)) {
return
} else if (compiledOptions.subtype === 'string' && typeof value !== 'string') {
return
}

const originalValue = compiledOptions.subtype === 'string' ? tip[field] || value : value
if (originalValue.length < compiledOptions.minLength) {
if (value.length < compiledOptions.minLength) {
errorReporter.report(pointer, RULE_NAME, DEFAULT_MESSAGE, arrayExpressionPointer, {
minLength: compiledOptions.minLength,
})
Expand Down
40 changes: 0 additions & 40 deletions test/validator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -737,46 +737,6 @@ test.group('Validator | validations with non-serialized options', () => {
})
})

test.group('Min Max Rules', () => {
test('min rule should check against the original value', async ({ assert }) => {
assert.plan(1)

try {
await validator.validate({
schema: schema.create({
username: schema.string({ escape: true }, [rules.minLength(5)]),
}),
data: {
username: '\\0',
},
})
} catch (error) {
assert.deepEqual(error.messages, { username: ['minLength validation failed'] })
}
})

test('min rule should check against the original nested value', async ({ assert }) => {
assert.plan(1)

try {
await validator.validate({
schema: schema.create({
profile: schema.object().members({
username: schema.string({ escape: true }, [rules.minLength(5)]),
}),
}),
data: {
profile: {
username: '\\0',
},
},
})
} catch (error) {
assert.deepEqual(error.messages, { 'profile.username': ['minLength validation failed'] })
}
})
})

test.group('After Before Field', () => {
test('fail when value is not after the defined field value', async ({ assert }) => {
assert.plan(1)
Expand Down

0 comments on commit 80081bb

Please sign in to comment.