title: Swift Combine create a custom Subject
body: <p>I would like to have one <code>Subject</code> (similar to <code>CurrentValueSubject</code>) I can publish into, but that validates the value I'm sending. For example, I would like to validate that the input is forced between a range of values, like 1 and 10. If higher than the max, pass the maximum, if lower then the min, pass the minimum.</p>
  
  <p>Please don't tell me to filter the result on the subscribing code because that's what I'm trying to avoid. That duplication.</p>
  
  <p>Pseudo code would be: </p>
  
  <pre class="lang-swift prettyprint-override"><code>let intSubject = ValidatedValueSubject&lt;Int&gt;(value: 5, min: 1, max: 10)
  
  intSubject.sink { value in  print(value) }
  
  intSubject.send(-10)
  intSubject.send(5)
  intSubject.send(15)
  </code></pre>
  
  <p>I would like this to produce: </p>
  
  <pre><code>5
  1
  5
  10
  </code></pre>
  
  <p>Obviously with <code>CurrentValueSubject</code> I can't achieve that effect. 
  I tried to create my own custom Subject but I can't seem to make it work. </p>
  
  <p>Something tells me I should look at my problem differently because I guess this is too easy to need a custom <code>Subject</code>.</p>
  
  <p>The use case: </p>
  
  <p>I have a settings class which is updated on a Settings screen, and everywhere else, when the value change I want the screens to react accordingly. The <code>ValidatedValueSubject</code> lives inside this Settings object. </p>
  
  <p>The Settings need to expose the <code>Subject</code> so any screens can react upon changes to the property. </p>
  
  <p>My approach to the custom <code>Subject</code>is as follows:</p>
  
  <pre class="lang-swift prettyprint-override"><code>final class QualitySubject: Subject {
  
      public typealias Output = Int
      public typealias Failure = Never
  
      public private(set) var value: Output
      private let max: Output
      private let min: Output
  
      init(value: Output, max: Output, min: Output) {
          self.min = min
          self.max = max
          self.value = value
          self.value = validated(value)
      }
  
      private func validated(_ value: Output) -&gt; Int {
          return max(min, min($0, max))
      }
  
      var subscription: [???? QualitySubscription ?????] = []
  
      public func send(_ value: Output) {
          self.value = validated(value)
          subscription.subscriber.receive(value)
      }
  
      public func send(completion: Subscribers.Completion&lt;Failure&gt;) {
          print("completion")
      }
  
      public func send(subscription: Subscription) {
          print("send subscription")
      }
  
  
      public func receive&lt;S&gt;(subscriber: S) where S : Subscriber, S.Failure == Failure, S.Input == Output {
          let qualitySubscription = QualitySubscription(value: value, subscriber: subscriber)
          subscriber.receive(subscription: qualitySubscription)
  
          // I think I should save a reference to the subscription so I could forward new values afterwards (on send method) but I can't because of generic constraints.
      }
  }
  
  </code></pre>