Sha256: e0ffc033a0f4178534324a6d013223507e4602b45c27cb651a775d2355a26b4e

Contents?: true

Size: 1.64 KB

Versions: 3

Compression:

Stored size: 1.64 KB

Contents

module Schemacop
  class Schema
    # Create a new Schema
    #
    # For detailed usage, please refer to README.md in the root of this project.
    #
    # @param args [Array] An array of arguments to apply to the root node of the
    #   Schema.
    # @param block A block with further Schema specification.
    # @raise [Schemacop::Exceptions::InvalidSchemaError] If the Schema defined
    #   is invalid.
    def initialize(*args, &block)
      @root = HashValidator.new do
        req :root, *args, &block
      end
    end

    # Query data validity
    #
    # @param data The data to validate.
    # @return [Boolean] True if the data is valid, false otherwise.
    def valid?(data)
      validate(data).valid?
    end

    # Query data validity
    #
    # @param data The data to validate.
    # @return [Boolean] True if data is invalid, false otherwise.
    def invalid?(data)
      !valid?(data)
    end

    # Validate data for the defined Schema
    #
    # @param data The data to validate.
    # @return [Schemacop::Collector] The object that collected errors
    #   throughout the validation.
    def validate(data)
      collector = Collector.new
      @root.fields[:root].validate({ root: data }, collector)
      return collector
    end

    # Validate data for the defined Schema
    #
    # @param data The data to validate.
    # @raise [Schemacop::Exceptions::ValidationError] If the data is invalid,
    #   this exception is thrown.
    # @return nil
    def validate!(data)
      collector = validate(data)

      unless collector.valid?
        fail Exceptions::ValidationError, collector.exception_message
      end

      return nil
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
schemacop-2.2.0 lib/schemacop/schema.rb
schemacop-2.1.0 lib/schemacop/schema.rb
schemacop-2.0.0 lib/schemacop/schema.rb