Locked video

Please purchase the course to watch this video.

Buy Now

Validation rules

isString

Summary

In Sails.js, use the isString validation rule to ensure that a ref or json attribute type strictly accepts strings. This is useful when you want the flexibility of a ref/json type (e.g., for future schema changes) but need to enforce string-only values for business logic. The validation triggers errors for non-string inputs (e.g., numbers, arrays) while allowing valid strings to pass.

Transcript

Let’s explore another validation rule: ensuring a ref or json attribute is a string. For example, consider a petName attribute defined as:

petName: {  
  type: 'ref',  
  isString: true  
}  

Here, type: 'ref' allows flexibility (e.g., storing strings, numbers, or objects), but isString: true enforces that the value must be a string.

To test this, attempt to create a record with invalid input (e.g., a number or array):

 User.create({ petName: 5 });  
User.create({ petName: ['Milo'] });  

Both cases throw errors like "Must be a string".

Valid input works as expected:

 User.create({ petName: 'Milo' });  

Key takeaways:

  • isString validation applies only to ref or json types.

  • Use this to enforce string constraints while retaining schema flexibility.

Full Course

$
34.99

USD

plus local taxes
Buy Now