N9I - 2/8/2010
Written on Tue, 09 Feb 2010 07:45:24 +0000 - Last updated on Tue, 09 Feb 2010 08:15:21 +0000A journal entry in the development of the N9I programming language, dated February 8, 2010
Annotations for Validation
In N9I, functions and methods may be marked with an annotation @validate that instructs N9I to perform some sort of check on a parameter. Validation may either "test" or "adjust" a parameter. Test means that the validation is expected to return true or false, and a value of false during "test" validation causes N9I to raise an error and refuse to enter the function/method. Adjust means that the validation alters the parameter itself, typically to sanitize it . Multiple validations may be performed on a parameter. For example, in the following function
#@validate method adjust method.toLowerCase()
#@validate method test ["get","post","put","delete"].contains(method)
#@validate url adjust url.replace(" ","%20")
public string httpRequest(string method, string url) ...
the function call httpRequest("get","foo bar") actually results in the parameters "get" and "foo%20bar" being passed in due to the "adjust" validation performed on the URL parameter. If "GET" were uppercase, the function would still recieve a lowercase "get" because of the first "adjust" validation which changes it to lower case. If the word "snatch" were passed in instead of "get", httpRequest() would trigger an error and refuse to execute ("snatch" fails the validation performed on the method parameter).