scotthovestadt / schema-object

Enforce schema on JavaScript objects, including type, transformation, and validation. Supports extends, sub-schemas, and arrays.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

custom validator functions?

dasantonym opened this issue · comments

commented

i might be missing something here and (i think) there was a discussion about stuff like that in #6, but i can't figure out how one might be able to implement custom validators. is that an existing feature? and if not, is that a deliberate design decision?

otherwise thanks for the great module, using it a lot!

@dasantonym thanks, I am really glad you like it.

what specifically are you trying to validate?

commented

thanks for the quick response! i need to verify iso durations and timestamps and check if a value is a uri or a specific version of a uuid.

since all those cases are string values i could probably use something akin to the "notemptystring" example and empty the value using a transform if it doesn't conform, but i thought i'd ask before doing this since it seems a bit hacky. what do you suggest in this case?

it's harder right now to generate error messages, since that wasn't really what I designed this code to do

my original use-case was something like this:

  • I have a giant mess of JSON/XML/CSV etc
  • I know the format I expect to have at the end of the process
  • I have 0 trust that the fields are there / typecasted / etc but I want the best possible result
  • I don't want to have to null check nested objects all of the time

error messages were added later by PR

if you want to just verify the value of a date you can use:

onValueSet (post-typecast value, clear after it is set)

transform (pre-typecast value, clear it here before it even gets set)

getter (let any value be set but control if and how it comes out when it is read)

more info on those params in the docs :)

commented

i see, thanks for the clarification! i first thought about forking and adding stuff but i guess this just goes towards "let's do mongoose without mongodb!" and unnecessarily bloats the module.

i guess i'll be good with what's already there and i have some ideas. just wanted to make sure i'm not missing anything. thanks again!