Skip to content

Instantly share code, notes, and snippets.

@mauricedb
Created April 27, 2014 17:04
Show Gist options
  • Save mauricedb/11350476 to your computer and use it in GitHub Desktop.
Save mauricedb/11350476 to your computer and use it in GitHub Desktop.
Validating data using ClearScript/V8 and JavaScript in the WebAPI
using (var engine = new V8ScriptEngine())
{
var settings = GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings;
var jsonBook = JsonConvert.SerializeObject(newBook, settings);
engine.Execute("var book = " + jsonBook);
var path = HttpContext.Current.Server.MapPath(@"~\App\ValidateBook.js");
engine.Execute(File.ReadAllText(path));
var result = engine.Evaluate("validateBook(book)") as string;
if (!string.IsNullOrEmpty(result))
{
return Request.CreateResponse(HttpStatusCode.NotAcceptable, result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment