Locked video

Please purchase the course to watch this video.

Buy Now

Validation rules

isNotEmptyString

Summary

In Waterline, an empty string is often considered a valid value for string data types. However, there are cases where an empty string should not be allowed, such as when storing a pet name. By default, Sails allows empty strings for string attributes, which might not be desirable.

To prevent empty strings, you can use the isNotEmptyString validation rule. This rule ensures that while an attribute may still be a string, it cannot be an empty string. This is particularly useful for required fields where an empty value should be rejected.

When attempting to create a record with an empty string, the validation will fail, but if a valid string is provided, it will pass successfully.

Transcript

So most times in Waterline, an empty string might be a valid value for the string data type. But I don't think you would want to have an empty string for something like a pet name like we do have here as a ref. And you could see we are validating that it's a string. So

Sails and Waterline are going to allow an empty string to be stored, and an empty string is valid, right? So with that said, if we do something like—right now, we're in Guppy—create petName, I'll give it an empty string here, we'll fetch it, and you could see we have an empty string, which is not what you want. You don't want an empty string for a pet name, right?

So what we need to do is to use the validation rule that does not allow for empty strings. How we do that is, in this petName, we could add isNotEmptyString. And this is valid on the ref data type. So it's isNotEmptyString. What we're saying is, though we are expecting a string, we do not want an empty string. All right? So you use it when you have a ref.

So you take a string, but an empty string is not a valid value for this attribute. That's what this means. So if we go right now to Guppy—

Now in Guppy, if we do await User.create({ petName: "" }) and we try to pass in an empty string,

you could see it's going to fail, right? Because we said we should not allow empty strings. So this is pretty useful when you don't want an empty string as a valid string. But if we do something like "Dojo", for example, you can see that that passes.

Full Course

$
34.99

USD

plus local taxes
Buy Now