Giter Site home page Giter Site logo

genome_challenge_validations's Introduction

Live Code Challenge

Write a program that validates a given input.

The validation criteria is the following.

The length of the string must be between 10 and 15 characters.
The string must include at least 3 numbers.
The string must include at least 4 uppercase characters.

When the criteria is not met, the program will throw a custom exception that has a message indicating the invalid criteria.

For example ,the string “too-short” will include the 3 messages.

[

  “The length of the input must be between 10 and 15 characters”,

  “The input must include at least 3 numbers”,

  “The input must include at least 4 uppercase characters”

]

We want a descriptive error message when the validation fails.

The code has to be implemented taking into account that the validation requirements may change over time.

We encourage you to write automated tests.

Solution

The proposed solution tries to mimic the way ActiveRecord::Validations works. This solution was implemented with Ruby 3.

Validator class hierarchy

Under Validator module there is a class hierarchy for describing different validators. Validator::Base class is the base class of the hierarchy and provides #validate method to be implemented in child classes.

For the sake of this exercise Validator::DigitsMinValidator, Validator::LengthInRangeValidator and Validator::UppercaseMinValidator are provided.

Validations module

For achieve this goal there is a Validations::Validatable module which add validate class method, and validate! instance method.

validate class method registers the attribute and the different validators to use depending on the validations.

validate! instance method run validations configured for each field and raise exception Validations::ValidationException with one or multiple error messages. The current implementation follows the description for this exercise and only includes the message, future implementations could include information on the field with the error.

There is a convention to be followed: When declaring a validation option, there should be a validator matching that option. Ex: for length_in_range the programs looks if exists a class in Validator module with the name LengthInRangeValidator.

Future versions could include other options for more flexibility.

Genome program

The class GenomeProgram includes the Validatable module and uses the validate class method for describing the validation on the input field. GenomeProgram instances has access to validate! method for runing validations.

require_relative './validations/validatable'

class GenomeProgram
  include Validations::Validatable

  attr_reader :input

  validate :input, length_in_range: (10..15), digits_min: 3, uppercase_min: 4

  def initialize(input)
    @input = input
  end
end

Test

Tests implemented in genome_program_test.rb file can be executed in a terminal using: ruby genome_program_test.rb. They cover the test for this exercise.

Those tests use Test::Unit Ruby framework.

genome_challenge_validations's People

Contributors

kellsaro avatar

Watchers

 avatar

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.