Skip to content

Instantly share code, notes, and snippets.

@kiichi
Last active August 29, 2015 14:22
Show Gist options
  • Select an option

  • Save kiichi/019ee72646e49001d8cc to your computer and use it in GitHub Desktop.

Select an option

Save kiichi/019ee72646e49001d8cc to your computer and use it in GitHub Desktop.
JavaScript base Custom Validator example in .NET
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace MyTest
{
public class eStatus
{
public class CustomValidationStatus
{
public string status { set; get; }
public string message { set; get; }
public ArrayList errors { get; set; }
}
}
}
using System;
using Jint;
using Jint.Native;
using Newtonsoft.Json;
using MyTest;
namespace JSTest {
class MainClass {
public static void Main (string[] args) {
Console.WriteLine ("Hello World!");
string script = @"
function customValidate(data){
var d = JSON.parse(data);
// do some validation...
if (d['hello'] == 'world'){
}
var result = {
'status':'error',
'message':'hello world',
'errors': ['test1','test3','test3']
};
return JSON.stringify(result);
}
";
var engine = new Engine().Execute(script);
JsValue jsval = engine.Invoke("customValidate", "{\"hello\":\"junk\"}");
eStatus.CustomValidationStatus status = JsonConvert.DeserializeObject<eStatus.CustomValidationStatus>(jsval.ToString());
Console.WriteLine (status.errors.Count + " errors detected: first error is ..." + status.errors[0]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment