I'm new to app-functions. But I have successfully created a custom app function that will accept some JSON.
When my JSON is uploaded, my app-function will want to validate it against a schema. The JSON will be in the body of the POST request to more accurately describe what is going on.
http://www.newtonsoft.com/json/help/html/JsonSchema.htm
Here is basic code for validation:
string schemaJson = @"{
'description': 'A person',
'type': 'object',
'properties':
{
'name': {'type':'string'},
'hobbies': {
'type': 'array',
'items': {'type':'string'}
}
}
}";
JsonSchema schema = JsonSchema.Parse(schemaJson);
JObject person = JObject.Parse(@"{
'name': 'James',
'hobbies': ['.NET', 'Blogging', 'Reading', 'Xbox', 'LOLCATS']
}");
IList<string> messages;
bool valid = person.IsValid(schema, out messages);
Where the above code has
string schemaJson
I would like to have a local file saved that I load and verify.
In actuality, I will have several files, and one of the parameters in the http-request will trigger which file I use to json-schema-validate.
Let's image that "mycustomerid" is passed in the header of the http-request (or a query-string or whatever), and the value for mycustomerid will drive which schema I want to validate the input json.
So I would have several files.
customer_1.jsonschema
customer_2.jsonschema
customer_3.jsonschema
What is the best-practice to store these files are necessary for my fairly simple app-function?
public static async Task<object> Run(HttpRequestMessage req, TraceWriter log)
{
APPEND:
This was my final answer using the accepted answer here AND what I found here: How to get local file system path in azure websites
string rootDirectory = string.Empty;
if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("HOME")))
{
/* running in azure */
rootDirectory = Environment.GetEnvironmentVariable("HOME") + "\\site\\wwwroot";
}
else
{
/* in visual studio, local debugging */
rootDirectory = ".";
}
string path = rootDirectory + "\\MySubFolder\\MyFile.jsonschema";