vapor/vapor

Using the `.in()` validator with a PartialRange never completes

lewisgodowski opened this issue · 1 comments

Describe the bug

I have a test that validates a JSON object query. One of the object's properties is an integer, and it's corresponding validation is as follows:

  static func validations(_ validations: inout Validations) {
    validations.add(.offset, as: Int.self, is: .in(0...))
  }

When the test runs, because the Sequence passed to .in() is unbounded, the test continues to run forever, racking up more and more memory:
Screenshot showing extreme memory usage of test

To Reproduce

Steps to reproduce the behavior:

  1. See above description.

Expected behavior

The .in() validation should not accept an unbounded or partial range.

Environment

  • Vapor Framework version: 4.77.2
  • Vapor Toolbox version: 18.7.1
  • OS version: 13.4.1

I'm aware that .range() should be used in this instance, but I think improved type checking here wouldn't hurt. Admittedly my experience with these particular underlying protocols is limited, but perhaps by limiting the input parameters to Collection instead of Sequence?

    public static func `in`<C>(_ collection: C) -> Validator<T>
        where C: Collection, C.Element == T

Although, then this particular method would essentially a duplicate of the other:

    public static func `in`(_ array: T...) -> Validator<T>