Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extend core types #79

Closed
skbolton opened this issue Jan 8, 2018 · 4 comments
Closed

extend core types #79

skbolton opened this issue Jan 8, 2018 · 4 comments
Labels
question Issues that are usage questions

Comments

@skbolton
Copy link

skbolton commented Jan 8, 2018

First off awesome library. You have saved me from the muck of json schema!
I find myself having to call superstruct a lot to add date and email validation. I was wondering if it is possible, or could be possible, to extend the core struct instance to have additonal types defined for all structs.
I was doing a special exported struct that I consumed but having to know and manage the require path is getting a tiny bit hairy. Would love your thoughts on this?

@ianstormtaylor
Copy link
Owner

@skbolton glad you like it!

Could you give a code sample showing what you mean?

@ianstormtaylor ianstormtaylor added the question Issues that are usage questions label Jan 8, 2018
@skbolton
Copy link
Author

skbolton commented Jan 8, 2018

I looked into the source and it would looks like it would be tough to add. But basically a way to add new types in without having to create a new struct out of a superstruct every time.
I have to write 12 of these structs and almost all of them need to have a date validation.

import { superstruct } from 'superstruct'
const struct = superstruct({
  types: {
    date: value => someDateVerifier(value)
})
// now use struct

Doing in this in every file was a little painful. So instead I did it in one file and all of my files using superstruct require that file. Its fine just means I have to manage some crazy require paths sometimes. I was hoping there could be a way to add types to extend the built in ones
Totally made up api incoming!

import { extendTypes } from 'superstruct'
extendTypes.addType('date', value => someDateVerification(value))

So now date verification is available to all superstructs

@ianstormtaylor
Copy link
Owner

Ah @skbolton I see what you mean now.

I agree that this would make that case easy, but Superstruct is actually designed purposefully to not allow extend the global behavior like that. Instead, you should do what you're already doing where you instantiate your own ./utils/superstruct (or similar) and require it wherever you need it.

I think although the require paths may be slightly frustrating (this can be fixed by having flatter/better code structures), it is very worth the explicitness of having the definitions all live in a single place, and not be globally mutable.

@skbolton
Copy link
Author

skbolton commented Jan 9, 2018

I totally agree with you. Simple and to the point is how this library can really stand out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Issues that are usage questions
Projects
None yet
Development

No branches or pull requests

2 participants