pax_global_header00006660000000000000000000000064125576112330014517gustar00rootroot0000000000000052 comment=3cb191936caab2507f358725f6d3eede1c66af93 ryotarai-schash-bbe5e0a/000077500000000000000000000000001255761123300153655ustar00rootroot00000000000000ryotarai-schash-bbe5e0a/.gitignore000066400000000000000000000001271255761123300173550ustar00rootroot00000000000000/.bundle/ /.yardoc /Gemfile.lock /_yardoc/ /coverage/ /doc/ /pkg/ /spec/reports/ /tmp/ ryotarai-schash-bbe5e0a/.rspec000066400000000000000000000000371255761123300165020ustar00rootroot00000000000000--format documentation --color ryotarai-schash-bbe5e0a/.travis.yml000066400000000000000000000000361255761123300174750ustar00rootroot00000000000000language: ruby rvm: - 2.2.1 ryotarai-schash-bbe5e0a/Gemfile000066400000000000000000000001331255761123300166550ustar00rootroot00000000000000source 'https://rubygems.org' # Specify your gem's dependencies in schash.gemspec gemspec ryotarai-schash-bbe5e0a/LICENSE.txt000066400000000000000000000020651255761123300172130ustar00rootroot00000000000000The 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. ryotarai-schash-bbe5e0a/README.md000066400000000000000000000042401255761123300166440ustar00rootroot00000000000000# 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 ryotarai-schash-bbe5e0a/Rakefile000066400000000000000000000002231255761123300170270ustar00rootroot00000000000000require "bundler/gem_tasks" begin require 'rspec/core/rake_task' RSpec::Core::RakeTask.new(:spec) rescue LoadError end task :default => :spec ryotarai-schash-bbe5e0a/bin/000077500000000000000000000000001255761123300161355ustar00rootroot00000000000000ryotarai-schash-bbe5e0a/bin/console000066400000000000000000000005131255761123300175210ustar00rootroot00000000000000#!/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 ryotarai-schash-bbe5e0a/bin/setup000066400000000000000000000001631255761123300172200ustar00rootroot00000000000000#!/bin/bash set -euo pipefail IFS=$'\n\t' bundle install # Do any other automated setup that you need to do here ryotarai-schash-bbe5e0a/lib/000077500000000000000000000000001255761123300161335ustar00rootroot00000000000000ryotarai-schash-bbe5e0a/lib/schash.rb000066400000000000000000000001721255761123300177310ustar00rootroot00000000000000require "schash/schema" require "schash/validator" require "schash/version" module Schash # Your code goes here... end ryotarai-schash-bbe5e0a/lib/schash/000077500000000000000000000000001255761123300174045ustar00rootroot00000000000000ryotarai-schash-bbe5e0a/lib/schash/schema.rb000066400000000000000000000001271255761123300211710ustar00rootroot00000000000000require 'schash/schema/error' require 'schash/schema/rule' require 'schash/schema/dsl' ryotarai-schash-bbe5e0a/lib/schash/schema/000077500000000000000000000000001255761123300206445ustar00rootroot00000000000000ryotarai-schash-bbe5e0a/lib/schash/schema/dsl.rb000066400000000000000000000015431255761123300217560ustar00rootroot00000000000000module 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 ryotarai-schash-bbe5e0a/lib/schash/schema/error.rb000066400000000000000000000001401255761123300223150ustar00rootroot00000000000000module Schash module Schema class Error < Struct.new(:position, :message); end end end ryotarai-schash-bbe5e0a/lib/schash/schema/rule.rb000066400000000000000000000004001255761123300221320ustar00rootroot00000000000000require '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' ryotarai-schash-bbe5e0a/lib/schash/schema/rule/000077500000000000000000000000001255761123300216135ustar00rootroot00000000000000ryotarai-schash-bbe5e0a/lib/schash/schema/rule/array_of.rb000066400000000000000000000012201255761123300237350ustar00rootroot00000000000000module 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 ryotarai-schash-bbe5e0a/lib/schash/schema/rule/base.rb000066400000000000000000000002301255761123300230450ustar00rootroot00000000000000module Schash module Schema module Rule class Base def optional? false # default end end end end end ryotarai-schash-bbe5e0a/lib/schash/schema/rule/hash.rb000066400000000000000000000014051255761123300230630ustar00rootroot00000000000000module 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 ryotarai-schash-bbe5e0a/lib/schash/schema/rule/match.rb000066400000000000000000000006241255761123300232360ustar00rootroot00000000000000module 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 ryotarai-schash-bbe5e0a/lib/schash/schema/rule/one_of_types.rb000066400000000000000000000007331255761123300246340ustar00rootroot00000000000000module 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 ryotarai-schash-bbe5e0a/lib/schash/schema/rule/optional.rb000066400000000000000000000004751255761123300237730ustar00rootroot00000000000000module 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 ryotarai-schash-bbe5e0a/lib/schash/schema/rule/type.rb000066400000000000000000000005721255761123300231250ustar00rootroot00000000000000module 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 ryotarai-schash-bbe5e0a/lib/schash/validator.rb000066400000000000000000000003501255761123300217140ustar00rootroot00000000000000module 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 ryotarai-schash-bbe5e0a/lib/schash/version.rb000066400000000000000000000000461255761123300214160ustar00rootroot00000000000000module Schash VERSION = "0.1.2" end ryotarai-schash-bbe5e0a/schash.gemspec000066400000000000000000000015361255761123300202100ustar00rootroot00000000000000# 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 ryotarai-schash-bbe5e0a/spec/000077500000000000000000000000001255761123300163175ustar00rootroot00000000000000ryotarai-schash-bbe5e0a/spec/schash_spec.rb000066400000000000000000000051611255761123300211320ustar00rootroot00000000000000require 'spec_helper' describe Schash::Validator do let(:schema) do proc do { data: { string: string, not_string: string, words: array_of(string), not_array: array_of(string), required_missing: string, optional_missing: optional(string), optional_type_error: optional(string), numeric: numeric, not_numeric: numeric, hash: { required_missing: string, }, array_of_hash: array_of({ required_missing: string, }), boolean: boolean, not_boolean: boolean, match: match(/^pattern$/), not_match: match(/^pattern$/) } } end end subject { described_class.new(&schema) } describe "#validate" do context "with invalid data" do it "returns errors" do errors = subject.validate({ data: { string: "string", not_string: 1, words: [1], not_array: 1, optional_type_error: 1, numeric: 1, not_numeric: "not numeric", hash: { }, array_of_hash: [{}], boolean: true, not_boolean: "string", match: "pattern", not_match: "not match pattern" }, }) expected = [ [ ["data", "not_string"], "is not String", ], [ ["data", "words", 0], "is not String", ], [ ["data", "not_array"], "is not an array", ], [ ["data", "required_missing"], "is required but missing", ], [ ["data", "optional_type_error"], "is not String", ], [ ["data", "not_numeric"], "is not Numeric", ], [ ["data", "hash", "required_missing"], "is required but missing", ], [ ["data", "array_of_hash", 0, "required_missing"], "is required but missing", ], [ ["data", "not_boolean"], "is not any of TrueClass, FalseClass", ], [ ["data", "not_match"], "does not match /^pattern$/", ] ] expect(errors.size).to eq(expected.size) errors.each_with_index do |error, i| expect(error.position).to eq(expected[i][0]) expect(error.message).to eq(expected[i][1]) end end end end end ryotarai-schash-bbe5e0a/spec/spec_helper.rb000066400000000000000000000001141255761123300211310ustar00rootroot00000000000000$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) require 'schash'