Skip to content

Instantly share code, notes, and snippets.

@AlexFrazer
Created May 22, 2015 16:33
Show Gist options
  • Save AlexFrazer/a0efb9fdd0a35d598ca9 to your computer and use it in GitHub Desktop.
Save AlexFrazer/a0efb9fdd0a35d598ca9 to your computer and use it in GitHub Desktop.
{
// type as in the primitive type of the data
type: Match.Any,
// label on the form
label: Match.Optional(Match.OneOf(String, Function)),
// is it required to be submitted or not
optional: Match.Optional(Match.OneOf(Boolean, Function)),
// these four are self-explanatory.
min: Match.Optional(Match.OneOf(Number, Date, Function)),
max: Match.Optional(Match.OneOf(Number, Date, Function)),
minCount: Match.Optional(Match.OneOf(Number, Function)),
maxCount: Match.Optional(Match.OneOf(Number, Function)),
// list of values that can be accepted
allowedValues: Match.Optional(Match.OneOf([Match.Any], Function)),
// if it's a number, does it have a decimal?
decimal: Match.Optional(Boolean),
exclusiveMax: Match.Optional(Boolean),
exclusiveMin: Match.Optional(Boolean),
// it must match this regex
regEx: Match.Optional(Match.OneOf(RegExp, [RegExp])),
// custom validation function
custom: Match.Optional(Function),
// whether or not to hide the keys of this object
blackbox: Match.Optional(Boolean),
// which value to make it by default
autoValue: Match.Optional(Function),
defaultValue: Match.Optional(Match.Any),
// trim the strings or not
trim: Match.Optional(Boolean),
// my extension to the library. Defines formatters from library.
fakeFormatter: Match.Optional(Function)
}
## Note: I have a package to generate random names
def generate_random(schema):
""" generate a matching random entity for this schema key """
if 'regex' in schema and 'fakeFormatter' not in schema:
return RandExp.generate(schema.regex)
elif 'fakeFormatter' in schema and 'regex' not in schema:
return Faker.generate(schema.fakeFormatter)
elif 'fakeFormatter' in schema and 'regex' in schema:
### this is where the problem comes in.
temp = Faker.generate(schema.fakeFormatter)
if not re.test(temp, schema.regex):
generate_random(schema)
else:
return temp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment