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("__", ":"); path = path.Replace("__", ":");
if (!_references.TryGetValue(path, out Type type)) if (!_references.TryGetValue(path, out Type type))
throw new ItemNotFoundException($"No configuration exists for the name: {path}"); 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); ExpandoObject config = ToObject(_configuration);
IDictionary<string, object> configDic = config; IDictionary<string, object> configDic = config;
// TODO validate the type
configDic[path] = value; configDic[path] = value;
JObject obj = JObject.FromObject(config); JObject obj = JObject.FromObject(config);
// TODO allow path to change await using StreamWriter writer = new(Program.JsonConfigPath);
await using StreamWriter writer = new("settings.json");
await writer.WriteAsync(obj.ToString()); await writer.WriteAsync(obj.ToString());
} }

View File

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

View File

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