-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModelBinder.cs
117 lines (86 loc) · 3.47 KB
/
ModelBinder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MongoDB.Bson.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Schema;
namespace AzureFunctions.ModelBinding
{
public static class ModelBinder
{
static Regex intRegex = new Regex("^[09]+$") ;
static Dictionary<Type, JSchema> schemas = new Dictionary<Type, JSchema>();
static BadRequestObjectResult defaultBadRequest = new BadRequestObjectResult(new string[] { "Invalid payload. Body should be a valid JSON Object" });
public static JsonSerializerSettings SerializationSettings = new JsonSerializerSettings();
public static void Register<T>(string name = null)
{
name = name ?? typeof(T).Name;
if (schemas.ContainsKey(typeof(T)))
return;
var binDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var rootDirectory = Path.GetFullPath(Path.Combine(binDirectory, ".."));
string json = File.ReadAllText(rootDirectory + "/Schemas/" + name + ".json");
schemas.Add(typeof(T), JSchema.Parse(json));
}
public static ModelState<T> Bind<T>(HttpRequest request)
{
if (request.Body == null)
{
return new ModelState<T>() {
IsValid = false,
BadRequest = defaultBadRequest
};
}
string requestBody = new StreamReader(request.Body).ReadToEnd();
return Bind<T>(requestBody);
}
public static ModelState<T> Bind<T>(string json)
{
IList<string> errorMessages = new List<string>();
ModelState<T> binding = new ModelState<T>()
{
IsValid = false
};
if (!schemas.ContainsKey(typeof(T)))
{
throw new ValidationSchemaNotRegisteredException<T>();
}
try
{
bool validRequest = JObject.Parse(json).IsValid(schemas[typeof(T)], out errorMessages);
if (!validRequest)
{
binding.BadRequest = new BadRequestObjectResult(errorMessages);
return binding;
}
binding.Model = BsonSerializer.Deserialize<T>(json);
binding.IsValid = true;
}
catch
{
binding.BadRequest = new BadRequestObjectResult(errorMessages);
}
return binding;
}
public static QueryModelState<T> BindQuery<T>(HttpRequest req)
{
QueryModelState<T> ret = new QueryModelState<T>();
if (req.Query.ContainsKey("limit") && intRegex.IsMatch(req.Query["limit"]))
ret.Limit = int.Parse(req.Query["limit"]);
if (req.Query.ContainsKey("offset") && intRegex.IsMatch(req.Query["offset"]))
ret.Offset = int.Parse(req.Query["offset"]);
if (ret.Limit > 100)
ret.Limit = 100;
ModelState<T> state = Bind<T>(req.QueryString.Value);
ret.IsValid = state.IsValid;
ret.BadRequest = state.BadRequest;
ret.Model = state.Model;
return ret;
}
}
}