question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[FEATURE] Conditional Validation

See original GitHub issue

Summary:

Would love if there was a way to run validation on an attribute if a different attribute is set to a specific value.

Code sample:

General

In a different package(https://github.com/clarkie/dynogels) that i’m using, it uses Joi where I can do this…

age: Joi.when('application_id', { is: 'foo', then: Joi.string().trim().required(), })

So this attribute is only required if application_id is set to foo. Doesn’t necessarily have to use Joi, thats just what that other package uses.

QA Discussion

Other:

  • I have read through the Dynamoose documentation before posting this issue
  • I have searched through the GitHub issues (including closed issues) and pull requests to ensure this feature has not already been suggested before
  • I have filled out all fields above
  • I am running the latest version of Dynamoose

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
fishcharliecommented, Dec 18, 2021

So based on my last comment.

Implement a validate method on the Schema itself

This is done, and will be included in the next v3 release (by the end of this weekend at the latest, if a new release hasn’t been released by the end of the weekend please remind me to do so).

Additionally (and kinda unrelated), there is support for get and set modifiers on the schema settings itself.

this will be a function that the item/document is passed into, or potentially an array of functions

This is a function that the item object is passed into. It does not currently accept an array of functions. If someone believes having an array of functions would be valuable, please create a new feature request issue for this.

Add support for plugins #851

Sadly, I have no idea if this is going to make it into v3. It’s really hard to say at this point.

dynamoose would build and maintain plugins for Joi support

See above. This is even less likely at this point.


Example:

const schema = new dynamoose.Schema({
	"id": String,
	"name": String
}, {
	"validate": (obj) => {
		if (!obj.id.beginsWith(name[0])) {
			throw new Error("id first letter of name.");
		}

		return true;
	}
});

So the short version: This is now possible. But not quite as seamless as it would be if Dynamoose had a Joi plugin.

So I will be closing this issue.


Disclaimer: I still have not used Joi before. Would appreciate someone testing out this new functionality to see if they can get it working, and report back if it’s successful or if the API needs to be changed or can be improved to better fit Joi validation.

2reactions
fishcharliecommented, Mar 15, 2021

After thinking about this a bit more. This seems like it’d be a good plugin for Dynamoose. In v3 I’m planning to explore basic plugin support.

In v3 I will either develop a plugin to support Joi validation, or support Joi validation natively.

Might also be interesting to explore having a validate function on the schema itself. That way you can validate the entire item and ensure the entire item is what you expect.

Read more comments on GitHub >

github_iconTop Results From Across the Web

feature: Conditional validation inside validation options #1721
Sometimes we need to validate a field based on another field in the dto. Proposed solution. it would be nice to have an...
Read more >
A Guide to Using Conditional Data Validation | Excelchat
Conditional data validation means restricting the choices in an Excel drop-down list depending on the value in another cell (or in another drop...
Read more >
3 ways to implement conditional validation of Angular reactive ...
We want clarity. Another way to achieve conditional validation in Angular forms would be to write your own group validator. This validator will...
Read more >
Conditional Validation for Elementor Pro Form - Dynamic.ooo
This feature helps you validate your Elementor Pro forms with simple conditional expressions. This will help to ensure that the form is submitted...
Read more >
Conditional validation | Drupal.org
Conditional validation is an available option to select after you choose a validator. As the name implies, if it is selected (and configured...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found