⚠️ This lesson is retired and might contain outdated information.

Validations Class

Brett Cassette
InstructorBrett Cassette
Share this video with your friends

Social Share Links

Send Tweet
Published 10 years ago
Updated 2 years ago

Now we bring the validations together with a Validations class.

Man 1: [00:00] The next bit of functionality in our validatable library is a simple but necessary one. That's the validation. A validation is a combination of a configured validation function from the previous step.

[00:12] Here we're configuring the main validator with a value of five, and the name of a field. The name in this case is actually just going to be Name, so we're going to validate the name of a user is at least five. This is similar to, again, using the syntax, user.validates length min five. Something like that.

[00:39] We're going to, in this case, insure that when we use the min validation to validate this user, that we get the value true. If we set the name to an empty string, that we get a false value, and also that the min validation exposes the message from the validation function.

[00:57] Right now, saving this out, we see that we have a failing test, because we have no validation class.

[01:03] Let's go ahead and add this validation class. Under "Validatable," we'll create a home for this. Validation. We'll copy out some of this to make it easier for us. Instead of "Validator," we're going to say, "Validation." Right now, this relies on nothing.

[01:30] There we go. Again, we just need to create a quick constructor for a validation, and we're going to return the validation. This takes, again, the field name and it takes a configured validation function. We're going to expose the public properties on the validation, so we'll say this.field equals field. This.message equals validation function.message. This.validate is a function that takes an instance and we're going to validate this instance by saying validation function and we'll pass in the instance, and we'll validate that field.

[02:12] Doing this, all of our tests pass, as we would expect. Rather simple. 

egghead
egghead
~ 7 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today