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

Dynamic Schema Generation #203

Closed
bradodarb opened this issue Mar 16, 2014 · 6 comments
Closed

Dynamic Schema Generation #203

bradodarb opened this issue Mar 16, 2014 · 6 comments
Labels
feature New functionality or improvement

Comments

@bradodarb
Copy link

This could be implemented outside of the core, but it would be nice to have a standard JSON format to create joi schemas on the fly.

Use case:
Say a client wants to define a new data type with validation. JSON interpretation would allow both the data and the schema to be saved in the db.

@hueniverse
Copy link
Contributor

Sounds like a fun project. I agree it doesn't belong in joi itself. I don't have any use cases for this but if someone comes with one, I'm happy to reopen the issue.

@felixfrtz
Copy link

If anyone stumbles across this, I have the exact same use case as OP describes. I have solved it using eval - granted - not pretty, but works very well.

const schemaObject = {};
if (type === 'string' ) {
	joiSchemaDescription += 'Joi.string()';
	if (min_length) {
		joiSchemaDescription += '.min(min_length)';
	}
	if (max_length) {
		joiSchemaDescription += '.max(max_length)';
	}
	if (required) {
		joiSchemaDescription += '.required()';
	}
	schemaObject[name] = eval(`${joiSchemaDescription};`);
}
// 'name' is also provided by the user, so you end up with a nice custom Schema like:
// schemaObject = {
//     someName: Joi.string().min(5).required(),
//     someOtherName: Joi.number().required();
// };
// Then validate like this:
// const fullSchema = Joi.object().keys(schemaObject);
// const validation = Joi.validate(data, fullSchema);

@bhargav50
Copy link

If anyone stumbles across this, I have the exact same use case as OP describes. I have solved it using eval - granted - not pretty, but works very well.

const schemaObject = {};
if (type === 'string' ) {
	joiSchemaDescription += 'Joi.string()';
	if (min_length) {
		joiSchemaDescription += '.min(min_length)';
	}
	if (max_length) {
		joiSchemaDescription += '.max(max_length)';
	}
	if (required) {
		joiSchemaDescription += '.required()';
	}
	schemaObject[name] = eval(`${joiSchemaDescription};`);
}
// 'name' is also provided by the user, so you end up with a nice custom Schema like:
// schemaObject = {
//     someName: Joi.string().min(5).required(),
//     someOtherName: Joi.number().required();
// };
// Then validate like this:
// const fullSchema = Joi.object().keys(schemaObject);
// const validation = Joi.validate(data, fullSchema);

Why we need eval (evil) here? Joi is chainable.

@bradodarb
Copy link
Author

@hueniverse I'd like to pick this back up, how best to go about it? A new project? A PR?

Thanks!

@hueniverse
Copy link
Contributor

See #1867

@hueniverse hueniverse added feature New functionality or improvement and removed request labels Sep 19, 2019
@lock
Copy link

lock bot commented Jan 9, 2020

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature New functionality or improvement
Projects
None yet
Development

No branches or pull requests

4 participants