Last active
March 20, 2020 15:26
-
-
Save ughitsaaron/dd497d12c3f2562d38eb77e9980f192f to your computer and use it in GitHub Desktop.
Is this a desirable API for a schema validation library?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// default schema factory | |
import schema from './lib'; | |
// each property type is exported separately with named validation helpers | |
import string, { max, min, matches, required } from './lib/string'; | |
import number from './lib/number'; | |
import shape from './lib/shape'; | |
import array, { every } from './lib/array'; | |
const validationSchema = | |
schema('schemaName', | |
shape( | |
string('username', [ | |
// a list of validation fns | |
min(1, 'optional error message'), | |
max(255, 'too long!'), | |
matches(/^[a-zA-Z0-9]+$/, 'is this even a valid regex? who knows!') | |
]), | |
array('friendIds', [ | |
every(number) | |
]), | |
// each type also accepts a generic function as a second argument | |
array('groups', (groups) => { | |
return schema('nestedSchema', shape( | |
string('groupName', required) | |
); | |
}) | |
) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment