Upgrade to Pro — share decks privately, control downloads, hide ads and more …

JSON Schemaでバックエンドエンジニアとフロントエンドエンジニアがコラボする / json-schema-collaboration

toshimaru
December 08, 2015

JSON Schemaでバックエンドエンジニアとフロントエンドエンジニアがコラボする / json-schema-collaboration

『エムスリー x Gunosy Beer bash』( http://gunosy-beer.connpass.com/event/22825/ ) での発表スライドです。

- 内容をまとめたBlog記事はこちら: http://blog.toshimaru.net/json-schema-collaboration/

toshimaru

December 08, 2015
Tweet

More Decks by toshimaru

Other Decks in Technology

Transcript

  1. JSON SchemaͰ
    όοΫΤϯυΤϯδχΞͱ
    ϑϩϯτΤϯυΤϯδχΞ͕
    ίϥϘ͢Δ

    View Slide

  2. Self Introduction
    » @toshimaru_e
    » Rails Developer @Gunosy
    » Gunosy Web (gunosy.com)
    » ! RailsΤϯδχΞืूத !

    View Slide

  3. JSON
    Schema

    View Slide

  4. JSON Schema஌ͬͯΔਓʁ

    View Slide

  5. JSON Schema࢖ͬͨ͜ͱ͋Δਓʁ

    View Slide

  6. JSON Schemaʹ2छྨ
    ͋Δ͜ͱ஌͍ͬͯΔਓʁ

    View Slide

  7. 2 Types of JSON Schema
    » JSON Schema
    » JSON Hyper-Schema

    View Slide

  8. JSON Schema
    » JSONͷ σʔλϑΥʔϚοτ Λهड़͢Δ
    » ਓؒʹ΋ػցʹ΋Θ͔Γ΍͍͢υΩϡϝϯτ
    » ϑΥʔϜͰαϒϛοτ͢ΔσʔλͷόϦσʔγϣϯʹ࢖͑Δ
    » ࣗಈςετʹ΋࢖͑Δ
    => ࠓ೔ͷ͓࿩͸͕ͬͪ͜ϝΠϯ

    View Slide

  9. JSON Hyper-Schema
    » Web APIͷ࢓༷ Λهड़͢Δ
    » APIͰظ଴͢ΔσʔλΛJSON SchemaͷܗࣜͰهड़
    » ೔ຊͰ͸ͬͪ͜ͷํ͕ϙϐϡϥʔʁ

    View Slide

  10. JSON SchemaͰ
    ίϥϘϨʔγϣϯ

    View Slide

  11. ίϥϘਤ
    +------------------+
    | |
    | Client-side JS |
    | (React) |
    | |
    +---+--------+-----+
    | ^
    | | +---------------+
    | JSON | <-------- | JSON Schema |
    | | +---------------+
    v |
    +---+--------+-----+
    | |
    | Server-side API |
    | (Rails) |
    | |
    +------------------+

    View Slide

  12. JSON Schema for us
    » For Humans
    » Clear specification
    » For Apps
    » useful for Validation
    » useful for Test
    » etc.

    View Slide

  13. For Humans

    View Slide

  14. Repositories
    » ! Rails API Repo !
    » "JSON Schema Repo#
    » $ Frontend Repo $

    View Slide

  15. Workflow
    1.ඞཁͳAPIͱͦ͜ʹؚ·ΕΔ΂͖σʔλΛચ͍ग़͠
    2.1ΛJSON Schemaʹམͱ͜͠Ή
    3.ϓϧϦΫʂ

    View Slide

  16. JSON Schema pull request

    View Slide

  17. GET /users/{id}

    View Slide

  18. # user.schema.yml
    $schema: http://json-schema.org/draft-04/schema#
    title: User
    description: An User
    type: object
    properties:
    id:
    type: integer
    email:
    type: string
    format: email
    name:
    type: string
    minLength: 1
    maxLength: 32
    required:
    - id
    - email
    - name

    View Slide

  19. For Apps

    View Slide

  20. JSON Schema Validation
    require 'json-schema'
    schema = {
    "type" => "object",
    "required" => ["a"],
    "properties" => {
    "a" => {"type" => "integer"}
    }
    }
    data = {
    "a" => 5
    }
    JSON::Validator.validate(schema, data)

    View Slide

  21. RSpec JSON Schema Matcher 1
    describe "Fetching the current user" do
    context "with valid auth token" do
    it "returns the current user" do
    user = create(:user)
    auth_header = { "Auth-Token" => user.auth_token }
    get v1_current_user_url, {}, auth_header
    expect(response.status).to eq 200
    expect(response).to match_response_schema("user")
    end
    end
    end
    1 Validating JSON Schemas with an RSpec Matcher

    View Slide

  22. Ruby JSON Schema Library
    » ruby-json-schema/json-schema
    » brandur/json_schema

    View Slide

  23. json-schema vs json_schema
    » depending on json-schema:
    » airbrake/airbrake
    » square/fdoc

    View Slide

  24. json-schema vs json_schema
    » depending on json_schema:
    » interagent/committee
    » interagent/prmd
    » increments/qiita-rb
    » r7kamura/rack-json_schema
    » r7kamura/jdoc

    View Slide

  25. JavaScript JSON Schema Library
    ฐࣾϑϩϯΤϯυΤϯδχΞͷΦεεϝ
    » mafintosh/is-my-json-valid
    » epoberezkin/ajv

    View Slide

  26. JSONॻ͘ͷπϥ͍໰୊
    » ʮด͡Χοί͕ʔ!!ʯ
    » ʮΧϯϚ͕ʔ!!!!ʯ
    » ʮίϝϯτ͕ʔ!!!!!!ʯ
    » ʮΫΦςʔγϣϯ͕ʔ!!!!!!ʯ

    View Slide

  27. ໧ͬͯYAMLͰॻ͜͏

    View Slide

  28. JSON Schemaͷࠓͱ͜Ε͔Β
    » json-schema/json-schema

    View Slide

  29. JSON Schemaͷ
    ࠓͱ͜Ε͔Β
    » JSON Schema v5 Proposals͕
    ग़͞Ε͍ͯΔஈ֊
    » Issue΍Google GroupsΛ௥͏ͱ
    Α͛͞
    » v5΁ͷ۩ମతͳϩʔυϚοϓ͸Ҿ͔
    Ε͍ͯͳ͍໛༷

    View Slide

  30. JSON SchemaίϥϘͰΑ͔ͬͨ͜
    ͱ
    » όοΫΤϯυΤϯδχΞͱϑϩϯτΤϯυΤϯδχΞͷ࢓༷ͷڞ௨ೝ

    » JSON SchemaΛઌʹఆ͓ٛͯ͘͜͠ͱͰόοΫΤϯυΤϯδχΞͱ
    ϑϩϯτΤϯυΤϯδχΞ͕ૄʹ։ൃͰ͖Δ
    » JSON SchemaͰόάͷࠞೖΛ๷͙

    View Slide

  31. ͋Γ͕ͱ͏͍͟͝·ͨ͠

    View Slide