Module: SimpleJSONSchema

Defined in:
lib/simple_json_schema.rb,
lib/simple_json_schema/cache.rb,
lib/simple_json_schema/scope.rb,
lib/simple_json_schema/checker.rb,
lib/simple_json_schema/version.rb,
lib/simple_json_schema/validator.rb,
lib/simple_json_schema/ref_helper.rb,
lib/simple_json_schema/items_helper.rb,
lib/simple_json_schema/regex_helper.rb,
lib/simple_json_schema/uri_extender.rb,
lib/simple_json_schema/custom_draft7.rb,
lib/simple_json_schema/validators/base.rb,
lib/simple_json_schema/validators/null.rb,
lib/simple_json_schema/properties_helper.rb,
lib/simple_json_schema/validators/number.rb,
lib/simple_json_schema/validators/string.rb,
lib/simple_json_schema/validators/boolean.rb,
lib/simple_json_schema/validators/integer.rb,
lib/simple_json_schema/validators/numeric.rb,
lib/simple_json_schema/concerns/hash_acessor.rb,
lib/simple_json_schema/validators/concerns/format.rb

Defined Under Namespace

Modules: Checker, Concerns, ItemsHelper, PropertiesHelper, RefHelper, RegexHelper, URIExtender, Validators Classes: Cache, Scope, UnsupportedMetaSchema, Validator

Constant Summary collapse

DEFAULT_SCHEMA =
'https://gitlab.com/vliper/simple_json_schema/-/raw/master/draft-07-schema#'
DRAFT_BY_SCHEMA =
{
  DEFAULT_SCHEMA => :draft7,
  'http://json-schema.org/draft-07/schema#' => :draft
}.freeze
NET_HTTP_REF_RESOLVER =
proc { |uri| JSON.parse(Net::HTTP.get(uri)) }
VERSION =
'0.1.7'
CUSTOM_DRAFT7 =

Draft-7 whit custom settings.

{
  '$schema' => 'https://gitlab.com/vliper/simple_json_schema/-/raw/master/draft-07-schema#',
  '$id' => 'https://gitlab.com/vliper/simple_json_schema/-/raw/master/draft-07-schema#',
  'title' => 'Simple JSON Schema meta-schema',
  'definitions' => {
    'schemaArray' => {
      'type' => 'array',
      'minItems' => 1,
      'items' => { '$ref' => '#' }
    },
    'nonNegativeInteger' => {
      'type' => 'integer',
      'minimum' => 0
    },
    'nonNegativeIntegerDefault0' => {
      'allOf' => [
        { '$ref' => '#/definitions/nonNegativeInteger' },
        { 'default' => 0 }
      ]
    },
    'simpleTypes' => {
      'enum' => %w[
        array
        boolean
        integer
        null
        number
        object
        string
      ]
    },
    'stringArray' => {
      'type' => 'array',
      'items' => { 'type' => 'string' },
      'uniqueItems' => true,
      'default' => []
    }
  },
  'type' => %w[object boolean],
  'properties' => {
    '$id' => {
      'type' => 'string',
      'format' => 'uri-reference'
    },
    '$schema' => {
      'type' => 'string',
      'format' => 'uri'
    },
    '$ref' => {
      'type' => 'string',
      'format' => 'uri-reference'
    },
    '$comment' => {
      'type' => 'string'
    },
    'title' => {
      'type' => 'string'
    },
    'description' => {
      'type' => 'string'
    },
    'default' => true,
    'readOnly' => {
      'type' => 'boolean',
      'default' => false
    },
    'writeOnly' => {
      'type' => 'boolean',
      'default' => false
    },
    'examples' => {
      'type' => 'array',
      'items' => true
    },
    'multipleOf' => {
      'type' => 'number',
      'exclusiveMinimum' => 0
    },
    'maximum' => {
      'type' => 'number'
    },
    'exclusiveMaximum' => {
      'type' => 'number'
    },
    'minimum' => {
      'type' => 'number'
    },
    'exclusiveMinimum' => {
      'type' => 'number'
    },
    'maxLength' => { '$ref' =>  '#/definitions/nonNegativeInteger' },
    'minLength' => { '$ref' =>  '#/definitions/nonNegativeIntegerDefault0' },
    'pattern' => {
      'type' => 'string',
      'format' => 'regex'
    },
    'additionalItems' => { '$ref' => '#' },
    'items' => {
      'anyOf' => [
        { '$ref' => '#' },
        { '$ref' => '#/definitions/schemaArray' }
      ],
      'default' => true
    },
    'maxItems' => { '$ref' =>  '#/definitions/nonNegativeInteger' },
    'minItems' => { '$ref' =>  '#/definitions/nonNegativeIntegerDefault0' },
    'uniqueItems' => {
      'type' => 'boolean',
      'default' => false
    },
    'contains' => { '$ref' => '#' },
    'maxProperties' => { '$ref' =>  '#/definitions/nonNegativeInteger' },
    'minProperties' => { '$ref' =>  '#/definitions/nonNegativeIntegerDefault0' },
    'required' => { '$ref' => '#/definitions/stringArray' },
    'additionalProperties' => { '$ref' => '#' },
    'definitions' => {
      'type' => 'object',
      'additionalProperties' => { '$ref' => '#' },
      'default' => {}
    },
    'properties' => {
      'type' => 'object',
      'additionalProperties' => { '$ref' => '#' },
      'default' => {}
    },
    'patternProperties' => {
      'type' => 'object',
      'additionalProperties' => { '$ref' => '#' },
      'propertyNames' => { 'format' => 'regex' },
      'default' => {}
    },
    'dependencies' => {
      'type' => 'object',
      'additionalProperties' => {
        'anyOf' => [
          { '$ref' => '#' },
          { '$ref' => '#/definitions/stringArray' }
        ]
      }
    },
    'propertyNames' => { '$ref' => '#' },
    'const' => true,
    'enum' => {
      'type' => 'array',
      'items' => true,
      'minItems' => 1,
      'uniqueItems' => true
    },
    'type' => {
      'anyOf' => [
        { '$ref' => '#/definitions/simpleTypes' },
        {
          'type' => 'array',
          'items' => { '$ref' => '#/definitions/simpleTypes' },
          'minItems' => 1,
          'uniqueItems' => true
        }
      ]
    },
    'format' => {
      'type' => 'string',
      # uri-template, was removed from `enum` to not be supported
      'enum' => %w[
        date-time
        date
        time
        email
        idn-email
        hostname
        idn-hostname
        ipv4
        ipv6
        uri
        uri-reference
        iri
        iri-reference
        json-pointer
        relative-json-pointer
        regex
      ]
    },
    # 'contentMediaType' => { 'type' => 'string' }, # not supported
    # 'contentEncoding' => { 'type' => 'string' }, # not supported
    'if' => { '$ref' => '#' },
    'then' => { '$ref' => '#' },
    'else' => { '$ref' => '#' },
    'allOf' => { '$ref' => '#/definitions/schemaArray' },
    'anyOf' => { '$ref' => '#/definitions/schemaArray' },
    'oneOf' => { '$ref' => '#/definitions/schemaArray' },
    'not' => { '$ref' => '#' },
    # Custom.
    'not_blank' => {
      'type' => 'boolean',
      'default' => false
    },
    'not_zero' => {
      'type' => 'boolean',
      'default' => false
    }
  },
  'default' => true
}.freeze

Class Method Summary collapse

Class Method Details

.valid(data, schema, options = nil) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/simple_json_schema.rb', line 50

def valid(data, schema, options = nil)
  if schema.is_a?(Hash)
    schema = schema.with_indifferent_access
    options ||= schema.delete(:options)
  end

  options = options.with_indifferent_access if options.is_a?(Hash)

  scope = Scope.new(data: data, schema: schema, draft: draft_class(schema), options: options)
  Validator.validate(scope)
  scope.errors
rescue StandardError => e
  scope.error(:invalid, exception: e.message)
end

.valid?(data, schema, options = nil) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
# File 'lib/simple_json_schema.rb', line 45

def valid?(data, schema, options = nil)
  errors = valid(data, schema, options)
  errors.none?
end