Skip to content
This repository was archived by the owner on Jul 3, 2020. It is now read-only.

Commit bffdcab

Browse files
fix
1 parent 02d6206 commit bffdcab

3 files changed

Lines changed: 17 additions & 16 deletions

File tree

EhTagApi/Controllers/DatabaseController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public ActionResult<Record> Delete(
180180
public ActionResult<Record> Post(
181181
[SingleNamespace] Namespace @namespace,
182182
[AcceptableRaw] string raw,
183-
[AcceptableTranslation, FromBody] Record record,
183+
[AcceptableRecord, FromBody] Record record,
184184
[FromHeader] User user)
185185
{
186186
var dic = _Database[@namespace];
@@ -201,7 +201,7 @@ public ActionResult<Record> Post(
201201
public ActionResult<Record> Put(
202202
[SingleNamespace] Namespace @namespace,
203203
[AcceptableRaw] string raw,
204-
[AcceptableTranslation, FromBody] Record record,
204+
[AcceptableRecord, FromBody] Record record,
205205
[FromHeader] User user)
206206
{
207207
var dic = _Database[@namespace];

EhTagApi/Controllers/ToolsController.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,21 @@ public IActionResult Status()
2929
}
3030

3131
[HttpPost("normalize")]
32-
public IActionResult Normalize([FromBody] Record record)
32+
public IActionResult Normalize([FromBody, AcceptableRecord(NoCheck = true)] Record record)
3333
{
34-
record.Render("");
3534
return Ok(record);
3635
}
3736

3837
[HttpPost("serialize/{raw}")]
3938
public IActionResult Serialize(
4039
[AcceptableRaw, Required] string raw,
41-
[FromBody] Record record)
40+
[FromBody, AcceptableRecord(NoCheck = true)] Record record)
4241
{
4342
return Ok(record.ToString(raw));
4443
}
4544

4645
[HttpPost("parse")]
47-
public IActionResult Parse([FromBody][Required] string tableRow)
46+
public IActionResult Parse([FromBody, Required] string tableRow)
4847
{
4948
var lines = tableRow.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
5049
if (lines.Length != 1)

EhTagClient/Record.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,21 @@
1010

1111
namespace EhTagClient
1212
{
13-
public sealed class AcceptableTranslationAttribute : ValidationAttribute
13+
public sealed class AcceptableRecordAttribute : ValidationAttribute
1414
{
15+
public bool NoCheck { get; set; }
16+
1517
public override bool RequiresValidationContext => true;
1618

1719
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
1820
{
1921
if (!(value is Record record))
20-
return new ValidationResult($"The value '{value}' is not valid.");
21-
var failedFields = new List<string>(2);
22+
return new ValidationResult($"The value '{value}' is not valid.", new[] { validationContext.DisplayName });
23+
record.Render(null);
24+
if (NoCheck)
25+
return ValidationResult.Success;
2226
if (string.IsNullOrEmpty(record.Name.Text))
23-
failedFields.Add(validationContext.MemberName + ".name.text");
24-
if (failedFields.Count != 0)
25-
return new ValidationResult($"Field should not be empty.", failedFields);
27+
return new ValidationResult($"Field should not be empty.", new[] { validationContext.DisplayName + ".name" });
2628
return ValidationResult.Success;
2729
}
2830
}
@@ -31,12 +33,12 @@ public sealed class AcceptableRawAttribute : ValidationAttribute
3133
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
3234
{
3335
if (!(value is string raw))
34-
return new ValidationResult($"The value '{value}' is not a valid tag.");
36+
return new ValidationResult($"The value '{value}' is not a valid tag.", new[] { validationContext.DisplayName });
3537
var traw = raw.Trim();
3638
if (traw != raw)
37-
return new ValidationResult($"The tag starts with or end with whitespaces.");
39+
return new ValidationResult($"The tag starts with or end with whitespaces.", new[] { validationContext.DisplayName });
3840
if (string.IsNullOrEmpty(traw))
39-
return new ValidationResult($"The tag is too short.");
41+
return new ValidationResult($"The tag is too short.", new[] { validationContext.DisplayName });
4042
foreach (var ch in traw)
4143
{
4244
if ((ch >= 'a' && ch <= 'z')
@@ -46,7 +48,7 @@ protected override ValidationResult IsValid(object value, ValidationContext vali
4648
|| ch == ' '
4749
|| ch == '-')
4850
continue;
49-
return new ValidationResult($"The tag included non-alphanumeric characters which are not permitted. Only hyphens, periods, and spaces are allowed in tags.");
51+
return new ValidationResult($"The tag included non-alphanumeric characters which are not permitted. Only hyphens, periods, and spaces are allowed in tags.", new[] { validationContext.DisplayName });
5052
}
5153
return ValidationResult.Success;
5254
}

0 commit comments

Comments
 (0)