Giter Site home page Giter Site logo

Question - Dependent Rules about valit HOT 3 CLOSED

rmszc81 avatar rmszc81 commented on June 11, 2024
Question - Dependent Rules

from valit.

Comments (3)

dbarwikowski avatar dbarwikowski commented on June 11, 2024

@rmszc81 Hey, thanks for question.
Currently there is no support for Dependent Rules.
Could you put more light on this topic?
I would like to get better understanding in which scenarios this is useful.

Regards,
DB

from valit.

GooRiOn avatar GooRiOn commented on June 11, 2024

As FV author says:

"Jeremy’s note: Personally I do not particularly like using DependentRules as I feel it’s fairly hard to read, especially with a complex set of rules. In many cases, it can be simpler to use When conditions to prevent rules from running in certain situations."

So in Valit there the similar way to acheive that using When() extension which eveluates validation rules only when given predicate returns true.

from valit.

rmszc81 avatar rmszc81 commented on June 11, 2024

@dbarwikowski, @GooRiOn yes, it's true, we can achieve that using the When clause but, in my specific case (and many other projects from past), we have two stages of validation.

  1. we validate if the objects has "correct values", not empty, not null, etc.
  2. we use these same objects to perform a database query to ensure the 2nd stage of the validation.

Real code, FV:

InlineValidatorUser
	.RuleFor(u => u)
	.NotEmpty().WithErrorCode(ErrorCodes.User.ObjectCannotBeEmpty)
	.DependentRules(() =>
	{
		InlineValidatorUser
			.RuleFor(u => u.Id)
			.Cascade(CascadeMode.StopOnFirstFailure)
			.NotEmpty().WithErrorCode(ErrorCodes.User.IdIsNotValid)
			.Must(IsValid).WithErrorCode(ErrorCodes.User.IdDoesNotExists);
		
		InlineValidatorUser
			.RuleFor(u => u.Email)
			.Cascade(CascadeMode.StopOnFirstFailure)
			.NotEmpty().WithErrorCode(ErrorCodes.User.EmailCannotBeEmpty)
			.MinimumLength(8).WithErrorCode(ErrorCodes.User.EmailMinLengthIs8)
			.MaximumLength(64).WithErrorCode(ErrorCodes.User.EmailMaxLengthIs64)
			.EmailAddress().WithErrorCode(ErrorCodes.User.EmailIsNotValid);
	
	}).Must(IsSame).WithErrorCode(ErrorCodes.User.EmailCannotBeChanged);

On this example, first, we validate that the user object is not empty/null. 2nd, we validate the Id and the Email address and here's is where the Dependent Rules come in: The IsSame method, who perform a database call, will be run only if the previous validations where successful.

So, this is something that I would like to see in Valit but I understand the complexity over the implementation like this.

Please let me know if I can provide more cases.

Thank you very much!

Regards,

[],

@rmszc81

from valit.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.