Skip to content

Commit

Permalink
Merge #4021 Further improve netkan relationship error message
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Feb 10, 2024
2 parents c91a46f + 8864b3b commit 05631a8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 40 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ All notable changes to this project will be documented in this file.

- [Policy] Fix #3518 rewrite de-indexing policy (#3993 by: JonnyOThan; reviewed: HebaruSan)
- [Netkan] Fix null reference exception in swinfo transformer (#3999 by: HebaruSan)
- [Netkan] Improve netkan relationship error message (#4020 by: HebaruSan)
- [Netkan] Improve netkan relationship error message (#4020, #4021 by: HebaruSan)

## v1.34.4 (Niven)

Expand Down
92 changes: 53 additions & 39 deletions Netkan/Validators/RelationshipsValidator.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Linq;

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

using CKAN.Versioning;
Expand All @@ -20,49 +19,64 @@ public void Validate(Metadata metadata)
{
throw new Kraken("spec_version v1.2+ required for 'supports'");
}
foreach (var rel in json[relName].Children<JObject>())
foreach (var relToken in json[relName].Children())
{
if (rel.ContainsKey("any_of"))
switch (relToken)
{
if (metadata.SpecVersion < v1p26)
{
throw new Kraken("spec_version v1.26+ required for 'any_of'");
}
foreach (string forbiddenPropertyName in AnyOfRelationshipDescriptor.ForbiddenPropertyNames)
{
if (rel.ContainsKey(forbiddenPropertyName))
case JObject rel:
if (rel.ContainsKey("any_of"))
{
throw new Kraken($"{forbiddenPropertyName} is not valid in the same relationship as 'any_of'");
if (metadata.SpecVersion < v1p26)
{
throw new Kraken("spec_version v1.26+ required for 'any_of'");
}
foreach (string forbiddenPropertyName in AnyOfRelationshipDescriptor.ForbiddenPropertyNames)
{
if (rel.ContainsKey(forbiddenPropertyName))
{
throw new Kraken($"{forbiddenPropertyName} is not valid in the same relationship as 'any_of'");
}
}
if (rel.ContainsKey("choice_help_text") && metadata.SpecVersion < v1p31)
{
throw new Kraken("spec_version v1.31+ required for choice_help_text in same relationship as 'any_of'");
}
foreach (var optToken in rel["any_of"].Children())
{
switch (optToken)
{
case JObject opt:
string name = (string)opt["name"];
if (!Identifier.ValidIdentifierPattern.IsMatch(name))
{
throw new Kraken($"{name} in {relName} 'any_of' is not a valid CKAN identifier");
}
break;
default:
throw new Kraken($"{relName} 'any_of' elements should be {JTokenType.Object}, found {optToken.Type}: {optToken.ToString(Formatting.None)}");
}
}
}
}
if (rel.ContainsKey("choice_help_text") && metadata.SpecVersion < v1p31)
{
throw new Kraken("spec_version v1.31+ required for choice_help_text in same relationship as 'any_of'");
}
foreach (JObject opt in rel["any_of"].Cast<JObject>())
{
string name = (string)opt["name"];
if (!Identifier.ValidIdentifierPattern.IsMatch(name))
else
{
throw new Kraken($"{name} in {relName} 'any_of' is not a valid CKAN identifier");
string name = (string)rel["name"];
if (!Identifier.ValidIdentifierPattern.IsMatch(name))
{
throw new Kraken($"{name} in {relName} is not a valid CKAN identifier");
}
if (rel.ContainsKey("version_min"))
{
throw new Kraken($"'version_min' found in relationship, the correct form is 'min_version'");
}
if (rel.ContainsKey("version_max"))
{
throw new Kraken($"'version_max' found in relationship, the correct form is 'max_version'");
}
}
}
}
else
{
string name = (string)rel["name"];
if (!Identifier.ValidIdentifierPattern.IsMatch(name))
{
throw new Kraken($"{name} in {relName} is not a valid CKAN identifier");
}
if (rel.ContainsKey("version_min"))
{
throw new Kraken($"'version_min' found in relationship, the correct form is 'min_version'");
}
if (rel.ContainsKey("version_max"))
{
throw new Kraken($"'version_max' found in relationship, the correct form is 'max_version'");
}
break;

default:
throw new Kraken($"{relName} elements should be {JTokenType.Object}, found {relToken.Type}: {relToken.ToString(Formatting.None)}");
}
}
}
Expand Down

0 comments on commit 05631a8

Please sign in to comment.