schash-0.1.2/0000755000175000017500000000000013007274464012044 5ustar scottscottschash-0.1.2/.travis.yml0000644000175000017500000000003613007274464014154 0ustar scottscottlanguage: ruby rvm: - 2.2.1 schash-0.1.2/schash.gemspec0000644000175000017500000000153613007274464014667 0ustar scottscott# coding: utf-8 lib = File.expand_path('../lib', __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'schash/version' Gem::Specification.new do |spec| spec.name = "schash" spec.version = Schash::VERSION spec.authors = ["Ryota Arai"] spec.email = ["ryota.arai@gmail.com"] spec.summary = %q{Ruby Hash validator} spec.homepage = "https://github.com/ryotarai/schash" spec.license = "MIT" spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } spec.bindir = "exe" spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } spec.require_paths = ["lib"] spec.add_development_dependency "bundler", "~> 1.8" spec.add_development_dependency "rake", "~> 10.0" spec.add_development_dependency "rspec" end schash-0.1.2/LICENSE.txt0000644000175000017500000000206513007274464013672 0ustar scottscottThe MIT License (MIT) Copyright (c) 2015 Ryota Arai Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. schash-0.1.2/Rakefile0000644000175000017500000000022313007274464013506 0ustar scottscottrequire "bundler/gem_tasks" begin require 'rspec/core/rake_task' RSpec::Core::RakeTask.new(:spec) rescue LoadError end task :default => :spec schash-0.1.2/.gitignore0000644000175000017500000000012713007274464014034 0ustar scottscott/.bundle/ /.yardoc /Gemfile.lock /_yardoc/ /coverage/ /doc/ /pkg/ /spec/reports/ /tmp/ schash-0.1.2/README.md0000644000175000017500000000424013007274464013323 0ustar scottscott# Schash [![Build Status](https://travis-ci.org/ryotarai/schash.svg?branch=master)](https://travis-ci.org/ryotarai/schash) Pronounciation: the same as "squash" Ruby hash validator ## Installation Add this line to your application's Gemfile: ```ruby gem 'schash' ``` And then execute: $ bundle ## Usage ```ruby validator = Schash::Validator.new do { nginx: { user: string, # required field worker_processes: optional(integer), # optional field sites: array_of({ server_name: string, root: string, allowed_ips: array_of(string), }), listen: match(/^(80|443)$/), }, } end # valid example valid = { nginx: { user: "www-data", worker_processes: 4, sites: [{ server_name: "example.com", root: "/var/www/itamae", allowed_ips: ["127.0.0.1/32"], }], listen: "80" }, } validator.validate(valid) # => [] # invalid example invalid = { nginx: { user: 123, sites: { server_name: "example.com", root: "/var/www/itamae", allowed_ips: ["127.0.0.1/32"], }, listen: "8080" }, } validator.validate(invalid) # => [#, #, #] ``` ## Development After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment. To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org). ## Contributing 1. Fork it ( https://github.com/ryotarai/schash/fork ) 2. Create your feature branch (`git checkout -b my-new-feature`) 3. Commit your changes (`git commit -am 'Add some feature'`) 4. Push to the branch (`git push origin my-new-feature`) 5. Create a new Pull Request schash-0.1.2/Gemfile0000644000175000017500000000013313007274464013334 0ustar scottscottsource 'https://rubygems.org' # Specify your gem's dependencies in schash.gemspec gemspec schash-0.1.2/bin/0000755000175000017500000000000013007274464012614 5ustar scottscottschash-0.1.2/bin/console0000644000175000017500000000051313007274464014200 0ustar scottscott#!/usr/bin/env ruby require "bundler/setup" require "schash" # You can add fixtures and/or initialization code here to make experimenting # with your gem easier. You can also use a different console, if you like. # (If you use this, don't forget to add pry to your Gemfile!) # require "pry" # Pry.start require "irb" IRB.start schash-0.1.2/bin/setup0000644000175000017500000000016313007274464013677 0ustar scottscott#!/bin/bash set -euo pipefail IFS=$'\n\t' bundle install # Do any other automated setup that you need to do here schash-0.1.2/.rspec0000644000175000017500000000003713007274464013161 0ustar scottscott--format documentation --color schash-0.1.2/lib/0000755000175000017500000000000013007274464012612 5ustar scottscottschash-0.1.2/lib/schash.rb0000644000175000017500000000017213007274464014410 0ustar scottscottrequire "schash/schema" require "schash/validator" require "schash/version" module Schash # Your code goes here... end schash-0.1.2/lib/schash/0000755000175000017500000000000013007274464014063 5ustar scottscottschash-0.1.2/lib/schash/schema/0000755000175000017500000000000013007274464015323 5ustar scottscottschash-0.1.2/lib/schash/schema/dsl.rb0000644000175000017500000000154313007274464016435 0ustar scottscottmodule Schash module Schema class DSL def self.evaluate(&block) self.new.instance_eval(&block) end def array_of(schema) Rule::ArrayOf.new(schema) end def one_of_types(*schemas) Rule::OneOfTypes.new(*schemas) end def type(klass) Rule::Type.new(klass) end def optional(rule) Rule::Optional.new(rule) end def string type(String) end def numeric type(Numeric) end def integer type(Integer) end def float type(Float) end def symbol type(Symbol) end def array type(Array) end def boolean one_of_types(TrueClass, FalseClass) end def match(pattern) Rule::Match.new(pattern) end end end end schash-0.1.2/lib/schash/schema/rule/0000755000175000017500000000000013007274464016272 5ustar scottscottschash-0.1.2/lib/schash/schema/rule/array_of.rb0000644000175000017500000000122013007274464020414 0ustar scottscottmodule Schash module Schema module Rule class ArrayOf < Base def initialize(rule) @rule = if rule.is_a?(::Hash) Rule::Hash.new(rule) else rule end end def validate(target, position = []) errors = [] unless target.is_a?(Array) errors << Error.new(position, "is not an array") return errors end errors += target.each_with_index.map do |t, i| @rule.validate(t, position + [i]) end.flatten errors end end end end end schash-0.1.2/lib/schash/schema/rule/hash.rb0000644000175000017500000000140513007274464017542 0ustar scottscottmodule Schash module Schema module Rule class Hash < Base def initialize(schema_hash) @schema_hash = schema_hash end def validate(target, position = []) @schema_hash.map do |key, rule| if rule.is_a?(::Hash) rule = self.class.new(rule) end found_key = [key.to_s, key.to_sym].find do |k| target.has_key?(k) end if found_key rule.validate(target[found_key], position + [key.to_s]) else unless rule.optional? Error.new(position + [key.to_s], "is required but missing") end end end.flatten.compact end end end end end schash-0.1.2/lib/schash/schema/rule/optional.rb0000644000175000017500000000047513007274464020452 0ustar scottscottmodule Schash module Schema module Rule class Optional < Base def initialize(rule) @rule = rule end def validate(target, position = []) @rule.validate(target, position) end def optional? true end end end end end schash-0.1.2/lib/schash/schema/rule/one_of_types.rb0000644000175000017500000000073313007274464021313 0ustar scottscottmodule Schash module Schema module Rule class OneOfTypes < Base def initialize(*klasses) @klasses = klasses end def validate(target, position = []) match = @klasses.any? do |klass| target.is_a?(klass) end if match [] else [Error.new(position, "is not any of #{@klasses.map(&:to_s).join(', ')}")] end end end end end end schash-0.1.2/lib/schash/schema/rule/type.rb0000644000175000017500000000057213007274464017604 0ustar scottscottmodule Schash module Schema module Rule class Type < Base def initialize(klass) @klass = klass end def validate(target, position = []) errors = [] unless target.is_a?(@klass) errors << Error.new(position, "is not #{@klass}") end errors end end end end end schash-0.1.2/lib/schash/schema/rule/match.rb0000644000175000017500000000062413007274464017715 0ustar scottscottmodule Schash module Schema module Rule class Match < Base def initialize(pattern) @pattern = pattern end def validate(target, position = []) errors = [] unless @pattern.match(target) errors << Error.new(position, "does not match #{@pattern.inspect}") end errors end end end end end schash-0.1.2/lib/schash/schema/rule/base.rb0000644000175000017500000000023013007274464017524 0ustar scottscottmodule Schash module Schema module Rule class Base def optional? false # default end end end end end schash-0.1.2/lib/schash/schema/error.rb0000644000175000017500000000014013007274464016774 0ustar scottscottmodule Schash module Schema class Error < Struct.new(:position, :message); end end end schash-0.1.2/lib/schash/schema/rule.rb0000644000175000017500000000040013007274464016611 0ustar scottscottrequire 'schash/schema/rule/base' require 'schash/schema/rule/array_of' require 'schash/schema/rule/one_of_types' require 'schash/schema/rule/hash' require 'schash/schema/rule/optional' require 'schash/schema/rule/type' require 'schash/schema/rule/match' schash-0.1.2/lib/schash/schema.rb0000644000175000017500000000012713007274464015650 0ustar scottscottrequire 'schash/schema/error' require 'schash/schema/rule' require 'schash/schema/dsl' schash-0.1.2/lib/schash/validator.rb0000644000175000017500000000035013007274464016373 0ustar scottscottmodule Schash class Validator def initialize(&schema_block) @validator = Schema::Rule::Hash.new(Schema::DSL.evaluate(&schema_block)) end def validate(target) @validator.validate(target) end end end schash-0.1.2/lib/schash/version.rb0000644000175000017500000000004613007274464016075 0ustar scottscottmodule Schash VERSION = "0.1.2" end