Last active
August 29, 2015 14:22
-
-
Save kiichi/019ee72646e49001d8cc to your computer and use it in GitHub Desktop.
JavaScript base Custom Validator example in .NET
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; } | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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