I'm a beginner in desing pattern and I wanna to refactor some of my old codes for practice.
The code below is basically a JSON schema validator, I retrieve the schemas (RestoreJsonSchemas method) through a folder with .txt files, containing the standard JSON.
Then I use these saved schemas to validate the data I am getting in the IsValidJson() method.
My question is how would it be possible to apply the Open/Closed principle? Or should I think of another design pattern?
public class JsonSchemaValidator
{
private const string connectionSchemaPath = "JsonSchemas\\schemaConnection.txt";
private string connectionJsonDefault;
public bool IsValidJson(string topic, string json)
{
try
{
if (topic != null && json != null)
{
JSchema schema;
JObject jsonObject = JObject.Parse(json);
switch (topic)
{
case "connection":
schema = JSchema.Parse(connectionJsonDefault);
return jsonObject.IsValid(schema);
/// more cases...
}
}
}
catch(Exception ex)
{
// to be implemented
return false;
}
return false;
}
public void RestoreJsonSchemas()
{
try
{
this.connectionJsonDefault = File.ReadAllText(connectionSchemaPath);
/// more files to restore
}
catch (Exception ex)
{
// to be implemented
}
Console.WriteLine("Restored all JSON schemas.");
}
}