How do I validate incoming JSON data inside a REST service?

I searched for the best practice to enforce validation for incoming json data into a RESTful service. My suggestion is to use a MessageBodyReader which performs the validation inside the readFrom method. Below there is an message-body-reader example which is non-generic for the sake of simplicity. I also was interesed in finding the best framework … Read more

Validate data using DataAnnotations with WPF & Entity Framework?

You can use the DataAnnotations.Validator class, as described here: http://johan.driessen.se/archive/2009/11/18/testing-dataannotation-based-validation-in-asp.net-mvc.aspx But if you’re using a “buddy” class for the metadata, you need to register that fact before you validate, as described here: http://forums.silverlight.net/forums/p/149264/377212.aspx TypeDescriptor.AddProviderTransparent( new AssociatedMetadataTypeTypeDescriptionProvider(typeof(myEntity), typeof(myEntityMetadataClass)), typeof(myEntity)); List<ValidationResult> results = new List<ValidationResult>(); ValidationContext context = new ValidationContext(myEntity, null, null) bool valid = Validator.TryValidateObject(myEntity, context, … Read more

ASP.Net Core MVC – Client-side validation for custom attribute

The IClientModelValidator is in fact the right interface. I’ve made a contrived sample implementation below. Attribute [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)] public sealed class CannotBeRedAttribute : ValidationAttribute, IClientModelValidator { public override bool IsValid(object value) { var message = value as string; return message?.ToUpper() == “RED”; } public void AddValidation(ClientModelValidationContext context) { MergeAttribute(context.Attributes, “data-val”, … Read more

In python, how to check if a date is valid?

You could try doing import datetime datetime.datetime(year=year,month=month,day=day,hour=hour) that will eliminate somethings like months >12 , hours > 23, non-existent leapdays (month=2 has max of 28 on non leap years, 29 otherwise, other months have max of 30 or 31 days)(throws ValueError exception on error) Also you could try to compare it with some sanity upper/lower … Read more

Validating XML against XSD [duplicate]

Returns simply true or false (also you don’t need any external library): static boolean validateAgainstXSD(InputStream xml, InputStream xsd) { try { SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = factory.newSchema(new StreamSource(xsd)); Validator validator = schema.newValidator(); validator.validate(new StreamSource(xml)); return true; } catch(Exception ex) { return false; } }

Why are Exceptions said to be so bad for Input Validation?

Reading these answers, I find it very unhelpful to say, “Exceptions should only be used for exceptional conditions”. This begs the whole question of what is an “exceptional condition”. This is a subjective term, the best definition of which is “any condition that your normal logic flow doesn’t deal with”. In other words, an exceptional … Read more

Validating url with jQuery without the validate-plugin?

You can use the same regex that the validation plugin does (updated to latest regex on May 23rd, 2015): function isUrlValid(url) { return /^(https?|s?ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&’\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&’\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&’\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&’\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&’\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(url); } var testCases = [ “http://www.google.com/”, “https://www.google.com/”, “ftp://www.google.com/”, “http://google.com/”, “http://google.com”, “https://google.com”, “http://www.google.com:80/”, “https://www.google.com:443/”, “http://127.0.0.1/”, “http://127.0.0.1:9200/”, “www.site.com”, “x:”, “http://”, “javascript:alert(‘xss’)”, “http://w”, “http:”, “derp://www.google.com”, “http://localserver” ], div = document.getElementById(“output”); for(var i=0; i < … Read more

On postback, how can I add a error message to validation summary?

Dynamically create a CustomValidator control and add it directly to the Page.Validators collection. Dim err As New CustomValidator err.ValidationGroup = “MyGroup” err.IsValid = False err.ErrorMessage = “The password is invalid” Page.Validators.Add(err) Unlike adding the CustomValidator to the markup, this method allows you to add any number of arbitrary error messages based on server-side business logic. … Read more