superstruct icon indicating copy to clipboard operation
superstruct copied to clipboard

Usage for other than types validations

Open vladshcherbin opened this issue 8 years ago • 13 comments

Hey

I checked the docs, but didn't find any info if this library is supposed to be used only to validate types of data or it can be used for other validations.

For example, I can check if value is a number. Is it possible and how to check, if this value is less than 100 or more than 6?

vladshcherbin avatar Dec 08 '17 07:12 vladshcherbin

You custom types for that.

poltib avatar Dec 08 '17 10:12 poltib

@poltib yep, I saw this in the docs, how will you create dynamic type where you can pass arguments? For example for length rule and its value 100 or other.

vladshcherbin avatar Dec 08 '17 10:12 vladshcherbin

Hey @VladShcherbin good question, can you explain your use case for the length check? Thanks!

ianstormtaylor avatar Dec 08 '17 17:12 ianstormtaylor

@ianstormtaylor I was interested in using this library as a validation library, here are some of typical type checks I'll need (with ref links about what they do):

  • min - to check min value
  • max - to check max value
  • between - to check if value is between min and max value
  • before - to check date
  • other dynamic types where you need to pass arguments

vladshcherbin avatar Dec 08 '17 18:12 vladshcherbin

There are similar rules (or type checks) in libraries, mentioned in the docs:

vladshcherbin avatar Dec 08 '17 18:12 vladshcherbin

@VladShcherbin what specific types of data are you looking to use min/max length checks for? This library is built differently than Joi/etc. and encourages different patterns. But I'm curious to hear what your use case needs them for before I write up an explanation assuming things.

ianstormtaylor avatar Dec 10 '17 06:12 ianstormtaylor

@ianstormtaylor I'd like to start at least with basic ones, strings and numbers - min/max/between for them.

vladshcherbin avatar Dec 10 '17 07:12 vladshcherbin

Another rule I use everywhere is unique where I can pass the database table name and column so I can use them to check value dynamically. Something like this: unique('articles', 'title'). I'm interested in this one too.

// looks something like this I guess
const articlesStruct = struct({
  title: struct.intersection(['string', 'alphaNum', max(30), unique('articles', 'title')])
})

vladshcherbin avatar Dec 10 '17 07:12 vladshcherbin

@VladShcherbin so I think the issue is that Superstruct encourages doing things a little differently. With other validation libraries (eg. Joi) you'll often define "types" inline, by doing things like:

username: Joi.string().alphanum().min(3).max(30).required()

Superstruct doesn't do this, by design. Instead it encourages you to define you data types upfront, in one place, and then refer to them elsewhere. So instead of defining the specific validation inline, you do something like:

const struct = superstruct({
  types: {
    username: v => typeof v === 'string' && v.length > 3 && v.length < 30 && /[a-zA-Z0-9]+/.test(v)
  }
})

const User = struct({
  id: 'number',
  name: 'string',
  username: 'username',
  ...
})

This way you have a single source of truth for data types, and can re-use them across objects. For example, you might call that data type login instead, and use it not just for users, but for many different objects in your database. I've found this to be a lot cleaner, and lead to easier to maintain code. (Of course, if you don't like this decision, the other validation libraries might be better for you.)

(Side note: this also encourages people to set max lengths on their data types, which is something that can be easily forgotten with other validators, and it critical for API reliability/security.)


If this is really not something you want, and you want to define things inline (which can be valid for certain use cases that have to be dynamic), you can have validation functions right in the struct definition, for example:

const User = struct({
  id: 'number',
  name: 'string',
  username: v => typeof v === 'string' && v.length > 3 && v.length < 30 && /[a-zA-Z0-9]+/.test(v),
  ...
})

That way you can get custom without having to define the type up front.


As for the unique database constraint, I wouldn't conflate that with validation, and would just let that fail at the database level personally. But check out #48 for the reason why Superstruct doesn't support async validations.


Hope that helps! I'm going to close this out as a question, but feel free to ask follow ups here if you have any other questions about this.

ianstormtaylor avatar Dec 10 '17 07:12 ianstormtaylor

@ianstormtaylor thank you for such a detailed answer, resolves all questions I have :)

vladshcherbin avatar Dec 10 '17 07:12 vladshcherbin

This is absolutely awful. You wrote a data validator which can't even do the most basic validations and to do them, I have to write practically the entire validation myself, and add on the extra things myself.

What on earth are you thinking? This lack of basic functionality makes this library worthless :/ If I have to write basic validations myself, then there is absolutely no benefit to using this library since practically EVERY REAL WORLD USE, would require this basic, yet missing feature. 2 years later.....

christhomas avatar Jun 04 '19 18:06 christhomas

@ianstormtaylor In love with Superstruct, but struggling a bit with this. How would you go about returning proper errors with this kind of validation? Something like "Value exceeds 80 characters". If we use a function that just returns false when any of the internal checks fail, then I don't know how create an error message.

frandiox avatar Dec 06 '19 05:12 frandiox

@frandiox good question, that part isn't super well documented, but if you're specifically looking to attach more information to your errors you can return failure object from the validators, not just booleans. But booleans are the 90% case.

Check out the Validator reference, which for now is the best place to see it. It's those Failure objects that can be returned.

ianstormtaylor avatar Dec 06 '19 15:12 ianstormtaylor