Skip to content

kawamanza/json-schematized

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JSON::Schematized

Object builder based on JSON-Schema.

Code badges:
Build Status Test Coverage Dependency Status

Project badges:
Gem Version Code Climate security

Sample usage

Consider the following JSON-Schema (escaped as YAML, for better viewing):

# person.yml
person:
  type: object
  properties:
    name:
      type: string
    birth:
      type: object
      properties:
        name:
          type: string
    children:
      type: array
      required: true
      items:
        type: object
        properties:
          name:
            type: string

Basic Wrapper Usage

require "json-schematized"

class Person < JSON::Schematized::Base
  json_schema do  # block called for each new instance
    YAML.load(File.read(File.expand_path("../person.yml", __FILE__)))["person"]
  end
end

person = Person.new name: "John", children: [{name: "William"}]
person.name                     # => "John"
person.children                 # => [{"name" => "William"}]
person.children.class           # => Person::ChildrenCollection
person.children.first.class     # => Person::Child
person.children.first.name      # => "William"

Another way to use Basic Wrapper is as follows:

class Person < Hash
  include JSON::Schematized
  json_schema wrapper: :basic do  # block called for each new instance
    YAML.load(File.read(File.expand_path("../person.yml", __FILE__)))["person"]
  end
end

Virtus Wrapper Usage

require "json-schematized"

class Person
  include JSON::Schematized
  json_schema wrapper: :virtus do  # block called only once
    YAML.load(File.read(File.expand_path("../person.yml", __FILE__)))["person"]
  end
end

person = Person.new name: "John", children: [{name: "William"}]
person.name                     # => "John"
person.children                 # => [#<Person::Child:0x007fc990906fd0 @name="William">]
person.children.class           # => Person::ChildrenCollection
person.children.first.class     # => Person::Child
person.children.first.name      # => "William"

Object with Basic Wrapper usage

person = Hash.new
json_schema = YAML.load(File.read(File.expand_path("../person.yml", __FILE__)))["person"]
person.extend JSON::Schematized::BasicWrapper.modularize(json_schema)

person.children << {}
# ...

About

Object builder based on JSON-Schema.

Resources

Stars

Watchers

Forks

Packages

 
 
 

Languages