Module: Saphyr

Defined in:
lib/saphyr.rb,
lib/saphyr/engine.rb,
lib/saphyr/fields.rb,
lib/saphyr/schema.rb,
lib/saphyr/asserts.rb,
lib/saphyr/helpers.rb,
lib/saphyr/version.rb,
lib/saphyr/validator.rb,
lib/saphyr/helpers/format.rb,
lib/saphyr/fields/ip_field.rb,
lib/saphyr/fields/b64_field.rb,
lib/saphyr/fields/uri_field.rb,
lib/saphyr/fields/url_field.rb,
lib/saphyr/fields/field_base.rb,
lib/saphyr/fields/array_field.rb,
lib/saphyr/fields/email_field.rb,
lib/saphyr/fields/float_field.rb,
lib/saphyr/asserts/base_assert.rb,
lib/saphyr/asserts/size_assert.rb,
lib/saphyr/fields/schema_field.rb,
lib/saphyr/fields/string_field.rb,
lib/saphyr/fields/boolean_field.rb,
lib/saphyr/fields/integer_field.rb,
lib/saphyr/asserts/string_assert.rb,
lib/saphyr/fields/datetime_field.rb,
lib/saphyr/fields/iso_lang_field.rb,
lib/saphyr/asserts/numeric_assert.rb,
lib/saphyr/asserts/error_constants.rb,
lib/saphyr/fields/iso_country_field.rb

Overview

Saphyr is a libray used to validate JSON data using a schema defined with a DSL.

Usage example :

Say you have a JSON literal string or already parsed ‘data’ like that :

json = '{"id": 3465, "name": "Bob"}'

data = {
  "id" => 3465,
  "name" => "Bob",
}

Define a Validator with a specific schema :

class ItemValidator < Saphyr::Validator
  schema do
    field :id, :integer, gte: 1, lt: 32000
    field :name, :string, min: 2, max: 50
  end
end

Validate data :

v = ItemValidator.new
if v.validate data
  puts "Validation : SUCCESS", "\n"
else
  puts "Validation : FAILED", "\n"
  Saphyr::Helpers::Format.errors_to_text v.errors
end

Or :

v = ItemValidator.new
if v.parse_and_validate json
  puts "Validation : SUCCESS", "\n"
  data = v.data                      # Get back the parsed json data
else
  puts "Validation : FAILED", "\n"
  Saphyr::Helpers::Format.errors_to_text v.errors
end

Defined Under Namespace

Modules: Asserts, Fields, Helpers Classes: Config, Engine, Error, Schema, Validator

Constant Summary collapse

VERSION =
'0.6.1'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configSaphyr::Config

The global configuration.

Returns:



54
55
56
# File 'lib/saphyr.rb', line 54

def config
  @config
end

Class Method Details

.global_schema(name) ⇒ Saphyr::Schema

Get a specific global schema.

Parameters:

  • name (Symbol)

    The name of the schema

Returns:

Raises:



70
71
72
# File 'lib/saphyr.rb', line 70

def global_schema(name)
  self.config.get_schema name
end

.register(&block) ⇒ Object

This method is part of the DSL used to register global field type or schema. ‘field_type’ and ‘schema’

Parameters:

  • block (Block)

    This block must use the followingDSL methods:



60
61
62
63
# File 'lib/saphyr.rb', line 60

def register(&block)
  self.config ||= Config.new
  self.config.instance_eval &block
end