Git Product home page Git Product logo

jsonapi-parser's Introduction

jsonapi-parser

Ruby gem for validating JSON API documents.

Status

Gem Version Build Status codecov Gitter chat

Resources

Installation

# In Gemfile
gem 'jsonapi-parser'

then

$ bundle

or manually via

$ gem install jsonapi-parser

Usage

First, require the gem:

require 'jsonapi/parser'

Then simply parse a document:

# This will raise JSONAPI::Parser::InvalidDocument if an error is found.
JSONAPI.parse_response!(document_hash)

or a resource create/update payload:

JSONAPI.parse_resource!(document_hash)

or a relationship update payload:

JSONAPI.parse_relationship!(document_hash)

License

jsonapi-parser is released under the MIT License.

jsonapi-parser's People

Contributors

beauby avatar richmolj avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

jsonapi-parser's Issues

Parsing vs Validating

When I see a method named parse with a payload as a parameter, I expect it to go through the payload and return a hash/object to better deal with the payload.
e.g.

Time <- Time.parse(string)
Hash <- JSON.parse(string)

It looks like this gem uses the parse to raise an exception if the payload is not a valid JSONAPI document.

Are there plans to add parsing functionality in this gem?

Document only works for hash - incompatible with Rails 5

I'm trying to use the jsonapi-rails gem to deserialize the incoming params on an HTTP request, but it's not working as expected.

I tried 2 approaches:

Firstly I followed the instructions from the guides and tried using the default JSONAPI::Deserializable::Resource.call(json_hash) like this:

class AuthorsController < ApplicationController
  def create
    respond_with Author.create(author_params), class: AuthorSerializer
  end

  private

  def author_params
    JSONAPI::Deserializable::Resource.call(params)  
  end

but since the JSONAPI::Parser::Resource is expecting a hash an error is raised.

The second approach is using the deserializable_resource on the controller level:

class AuthorsController < ApplicationController
  deserializable_resource :author, only: :create
  
  def create
    respond_with Author.create(author_params), class: AuthorSerializer
  end

  private

  def author_params
    params.require(:author).permit(:name)
  end

When I try it like this I get ActionController::ParameterMissing: param is missing or the value is empty: author when inspecting the author_params. If I inspect the params they are looking like this:

ActionController::Parameters {"data"=>{"type"=>"authors", "attributes"=>{"name"=>"Author name"}}, "controller"=>"api/v1/authors", "action"=>"create"} permitted: false>

which means that the desiralization on the params didn't occur.

Any guidance would be helpful. I'm using ruby 2.3.1p112 and Rails 5.0.1

Parser returns nil?

Perhaps I'm using this incorrectly. I would imagine JSONAPI.parse_response!(payload) returns a parsed response. If I open up IRB, this is what I see.

require 'jsonapi/parser'

#taken from specs
payload = {'data' => [{'type' => 'articles', 'id' => '1', 'attributes' => { 'title' => 'JSON API paints my bikeshed!' }, 'links' => { 'self' => 'http://example.com/articles/1' }, 'relationships' => {'author' => {'links' => {'self' => 'http://example.com/articles/1/relationships/author', 'related' => 'http://example.com/articles/1/author'}, 'data' => { 'type' => 'people', 'id' => '9' } }, 'journal' => {'data' => nil }, 'comments' => {'links' => {'self' => 'http://example.com/articles/1/relationships/comments', 'related' => 'http://example.com/articles/1/comments'}, 'data' => [{ 'type' => 'comments', 'id' => '5' }, { 'type' => 'comments', 'id' => '12' } ] } } } ], 'meta' => { 'count' => '13' } }


irb(main):005:0> JSONAPI.parse_response!(payload)
=> nil

Is this project dead?

Hi all, thanks for all the work on this project. Could you please respond if this project is alive? Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.