1

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";
Janusz Nowak
  • 2,595
  • 1
  • 17
  • 36
granadaCoder
  • 26,328
  • 10
  • 113
  • 146

1 Answers1

3

Step 1. Create a file 'schema.json' in 'D:\home\site\wwwroot\HttpTriggerCSharp1' using Kudu Console https://github.com/projectkudu/kudu/wiki/Kudu-console

Step 2. Code for your HttpTrigger to read file content as string:

using System.Net;

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
    var path = Environment.GetEnvironmentVariable("HOME").ToString() + "\\site\\wwwroot\\HttpTriggerCSharp1\\schema.json";

    string readText = File.ReadAllText(path);

    log.Info(readText);
    return req.CreateResponse(HttpStatusCode.OK, "Hello ");
}
Alexey Rodionov
  • 1,436
  • 6
  • 8