Last active
November 19, 2018 23:54
-
-
Save cotocisternas/03d83ff30598d42b2b216be023b8e616 to your computer and use it in GitHub Desktop.
Matrix Validation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # frozen_string_literal: true | |
| require 'boo/validation' | |
| module API | |
| module Validations | |
| module Params | |
| Search = Boo::Validation.Params do | |
| configure do | |
| config.type_specs = true | |
| end | |
| required(:country, :string).filled(:country?) | |
| required(:iss, :string).filled(:str?) | |
| required(:sub, :string).filled(:str?) | |
| required(:typ, Types::Unknown).maybe(:str?) | |
| optional(:id).maybe(:str?) | |
| optional(:exp).maybe(:str?) | |
| validate(valid_iss?: [:country, :iss]) do |country, iss| | |
| iss?(country, iss) | |
| end | |
| validate(valid_typ?: [:country, :iss, :typ]) do |country, iss, typ| | |
| typ.nil? ^ typ?(country, iss, typ) | |
| end | |
| end | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # frozen_string_literal: true | |
| require 'dry/struct' | |
| require 'dry/types' | |
| Dry::Types.load_extensions(:maybe) | |
| module Types | |
| include Dry::Types.module | |
| Unknown = Types::String.constructor do |str| | |
| str == 'unknown' ? nil : str | |
| end | |
| Name = Types::String.constructor do |str| | |
| str ? str.strip.chomp : str | |
| end | |
| Sym = Types::Symbol.constructor do |str| | |
| str ? str.to_sym : str | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # frozen_string_literal: true | |
| require 'dry/validation' | |
| # Boo::Container['config'].profile.credentials | |
| # Hash[country][issuer] = ['valid', 'credential', 'type'] | |
| module Boo | |
| class Validation < Dry::Validation::Schema | |
| configure do |config| | |
| option :credentials, Boo::Container['config'].profile.credentials | |
| config.messages_file = "#{Boo.root}/config/locales/en.yml" | |
| config.messages = :i18n | |
| end | |
| def country?(value) | |
| credentials.key? value | |
| end | |
| def iss?(country, value) | |
| credentials[country].key? value | |
| rescue NoMethodError | |
| false | |
| end | |
| def typ?(country, iss, value) | |
| credentials[country][iss].include? value | |
| rescue NoMethodError | |
| false | |
| end | |
| def email?(value) | |
| !/\A([\w+\-].?)+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i.match(value).nil? | |
| end | |
| def sym?(value) | |
| value.is_a?(Symbol) | |
| end | |
| def self.Params(&block) # rubocop:disable Naming/MethodName | |
| Dry::Validation.Params(Validation, &block) | |
| end | |
| def self.Schema(&block) # rubocop:disable Naming/MethodName | |
| Dry::Validation.Schema(Validation, &block) | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment