Mapping Validation Attributes From Domain Entity to DTO

If you’re using something supporting DataAnnotations, you should be able to use a metadata class to contain your validation attributes: public class ProductMetadata { [NotEmpty, NotShorterThan10Characters, NotLongerThan100Characters] public string Name { get; set; } [NotLessThan0] public decimal Price { get; set;} } and add it in the MetadataTypeAttribute on both the domain entity & DTO: … Read more

How to validate a passport? [closed]

I don’t think you’re going to be able to do this without restricting yourself to certain issuing countries. If you look at the specs of machine readable passports: http://www.highprogrammer.com/alan/numbers/mrp.html It says that each country is free to use any format it likes for the actual passport number (including letters or digits). Although in the machine … Read more

What is the difference between Invariants and Validation Rules?

Absolutely, validation is the process of approving a given object state, while invariant enforcement happens before that state has even been reached. A corollary is that invariant enforcement is best performed by the thing that is being mutated (or created) itself, like a self-protection reflex, whereas validation is usually done by a third party. The … Read more

How to reduce cyclomatic complexity?

An easy way is to promote the check into a separate method: private String getAppendString(String value, String appendString) { if (value == null || value.isEmpty()) { return “”; } return appendString; } And then you can use this method instead of the if blocks: sb.append(getAppendString(request.getStreet(), “street,”); This will reduce complexity from 28 down to 3. … Read more

How can I use the Data Validation Attributes in C# in a non-ASP.net context?

Actually this is pretty cool. I used it in a WFP validation implementation recently. Most people end up writing lots of code using reflection to iterate the attributes, but there’s a built in function for this. var vc = new ValidationContext(myObject, null, null); return Validator.TryValidateObject(myObject, vc, null, true); You can also validate attributes on a … Read more

How to run reCaptcha ONLY if HTML5 validation has passed?

Instead of attaching the reCAPTCHA attributes to the button directly you have to add them to a div and then use grecaptcha.execute(); on form submit. <script src=”https://www.google.com/recaptcha/api.js” async defer></script> <form id=”myForm”> Name: (required) <input id=”field” name=”field” required> <div id=’recaptcha’ class=”g-recaptcha” data-sitekey=”your_site_key” data-callback=”onCompleted” data-size=”invisible”></div> <button id=’submit’>submit</button> </form> <script> $(‘#myForm’).submit(function(event) { console.log(‘validation completed.’); event.preventDefault(); //prevent form submit … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)