Coder Social home page Coder Social logo

mtkachenk0 / paradocs Goto Github PK

View Code? Open in Web Editor NEW
8.0 2.0 4.0 473 KB

Ruby validation and documentation generation. Write validation code and get generated documentation that you don't have to maintain!

License: Other

Ruby 100.00%
ruby schema data-schema paradocs form-validation metadata coercion documentation generation json

paradocs's Introduction

Paradocs: Extended Parametric gem + Documentation Generation

Ruby

Declaratively define data schemas in your Ruby objects, and use them to whitelist, validate or transform inputs to your programs.

Useful for building self-documeting APIs, search or form objects. Or possibly as an alternative to Rails' strong parameters (it has no dependencies on Rails and can be used stand-alone).

Installation

$ gem install paradocs

Or with Bundler in your Gemfile.

gem 'paradocs'

Getting Started

Define a schema

schema = Paradocs::Schema.new do
  field(:title).type(:string).present
  field(:status).options(["draft", "published"]).default("draft")
  field(:tags).type(:array)
end

Populate and use. Missing keys return defaults, if provided.

form = schema.resolve(title: "A new blog post", tags: ["tech"])

form.output # => {title: "A new blog post", tags: ["tech"], status: "draft"}
form.errors # => {}

Learn more

Please read the documentation

paradocs's People

Contributors

idanci avatar ismasan avatar mtkachenk0 avatar ston1x avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

paradocs's Issues

Implement idle policy

Idle policy is a policy that doesn't raise any error and passes validation anyways, but executes a block if the validation logic actually failed.

Any policy should be able to be run as idle_policy

Implement field alias Field#as

For API that have logic changes inside but the structures cannot be modified (backward compatibility) would be usefull to implement method as for field

schema do 
  field(:from_id).present.as(:from_identifier)
end 

the result should not contain from_id but contain from_identifier with the very value of from_id

# Input:
{from_id: 1}
# Output:
{from_identifier: 1}

Allow passing descriptions for field

Example:

schema do 
  field(:name).type(:string).description("John")
  field(:age).type(:number).description("This field represents your age")
end

Generate draft schemas from objects

It can be used for faster enrollment. a method which takes an object and outputs somewhere (STDOUT) a Paradocs::Schema declaration syntax that matches the given schema

  • save the nesting
  • detect type of the field
  • all the given fields are present (?)

Allow passing example for field

It can be used for payload generation.

schema do 
  field(:name).type(:string).example("John")
  field(:age).type(:number).example(23)
  field(:gender).type(:string).options(:m, :f) # example can be random from the options unless defined
  field(:subscribe).type(:boolean).default(:true) # example can be default unless defined
end

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.