Handling type validation on config edits

This commit is contained in:
Zoe Roux 2021-05-19 23:09:43 +02:00
parent f3e415a050
commit fcd419993a
3 changed files with 14 additions and 4 deletions

View File

@ -41,14 +41,15 @@ namespace Kyoo.Controllers
path = path.Replace("__", ":");
if (!_references.TryGetValue(path, out Type type))
throw new ItemNotFoundException($"No configuration exists for the name: {path}");
value = JObject.FromObject(value).ToObject(type);
if (value == null)
throw new ArgumentException("Invalid value format.");
ExpandoObject config = ToObject(_configuration);
IDictionary<string, object> configDic = config;
// TODO validate the type
configDic[path] = value;
JObject obj = JObject.FromObject(config);
// TODO allow path to change
await using StreamWriter writer = new("settings.json");
await using StreamWriter writer = new(Program.JsonConfigPath);
await writer.WriteAsync(obj.ToString());
}

View File

@ -15,6 +15,11 @@ namespace Kyoo
/// </summary>
public static class Program
{
/// <summary>
/// The path of the json configuration of the application.
/// </summary>
public const string JsonConfigPath = "./settings.json";
/// <summary>
/// Main function of the program
/// </summary>
@ -66,7 +71,7 @@ namespace Kyoo
/// <returns>The modified configuration builder</returns>
private static IConfigurationBuilder SetupConfig(IConfigurationBuilder builder, string[] args)
{
return builder.AddJsonFile("./settings.json", false, true)
return builder.AddJsonFile(JsonConfigPath, false, true)
.AddEnvironmentVariables()
.AddCommandLine(args);
}

View File

@ -87,6 +87,10 @@ namespace Kyoo.Api
{
return NotFound();
}
catch (ArgumentException)
{
return BadRequest();
}
}
}
}