Sha256: 696627348a0ccd091096049197fd698bc11f1766ff464ac7d6aa61b6b9f6d0e5

Contents?: true

Size: 776 Bytes

Versions: 14

Compression:

Stored size: 776 Bytes

Contents

# frozen_string_literal: true

require "dry/validation"
require "refinements/array"

module Tana
  module Requests
    # Validates an API add request.
    class Add < Dry::Validation::Contract
      using Refinements::Array

      TARGETS = ["INBOX", "SCHEMA", /\A[0-9a-z\-_]{12}\Z/i].freeze

      def initialize(targets: TARGETS, **)
        @targets = targets
        super(**)
      end

      json do
        optional(:targetNodeId).filled :string
        required(:nodes).value(:array, min_size?: 1).each { hash LevelA }
      end

      rule :targetNodeId do
        next unless key? && targets.none? { |target| value.match? target }

        key.failure "is invalid. Use: #{targets.to_usage :or}"
      end

      private

      attr_reader :targets
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
tana-0.14.0 lib/tana/requests/add.rb
tana-0.13.0 lib/tana/requests/add.rb
tana-0.12.0 lib/tana/requests/add.rb
tana-0.11.0 lib/tana/requests/add.rb
tana-0.10.0 lib/tana/requests/add.rb
tana-0.9.0 lib/tana/requests/add.rb
tana-0.8.0 lib/tana/requests/add.rb
tana-0.7.0 lib/tana/requests/add.rb
tana-0.6.0 lib/tana/requests/add.rb
tana-0.4.0 lib/tana/requests/add.rb
tana-0.3.0 lib/tana/requests/add.rb
tana-0.2.0 lib/tana/requests/add.rb
tana-0.1.0 lib/tana/requests/add.rb
tana-0.0.0 lib/tana/requests/add.rb