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

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
OpportunityLiu committed Jun 5, 2020
1 parent 02d6206 commit bffdcab
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
4 changes: 2 additions & 2 deletions EhTagApi/Controllers/DatabaseController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public ActionResult<Record> Delete(
public ActionResult<Record> Post(
[SingleNamespace] Namespace @namespace,
[AcceptableRaw] string raw,
[AcceptableTranslation, FromBody] Record record,
[AcceptableRecord, FromBody] Record record,
[FromHeader] User user)
{
var dic = _Database[@namespace];
Expand All @@ -201,7 +201,7 @@ public ActionResult<Record> Post(
public ActionResult<Record> Put(
[SingleNamespace] Namespace @namespace,
[AcceptableRaw] string raw,
[AcceptableTranslation, FromBody] Record record,
[AcceptableRecord, FromBody] Record record,
[FromHeader] User user)
{
var dic = _Database[@namespace];
Expand Down
7 changes: 3 additions & 4 deletions EhTagApi/Controllers/ToolsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,21 @@ public IActionResult Status()
}

[HttpPost("normalize")]
public IActionResult Normalize([FromBody] Record record)
public IActionResult Normalize([FromBody, AcceptableRecord(NoCheck = true)] Record record)
{
record.Render("");
return Ok(record);
}

[HttpPost("serialize/{raw}")]
public IActionResult Serialize(
[AcceptableRaw, Required] string raw,
[FromBody] Record record)
[FromBody, AcceptableRecord(NoCheck = true)] Record record)
{
return Ok(record.ToString(raw));
}

[HttpPost("parse")]
public IActionResult Parse([FromBody][Required] string tableRow)
public IActionResult Parse([FromBody, Required] string tableRow)
{
var lines = tableRow.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
if (lines.Length != 1)
Expand Down
22 changes: 12 additions & 10 deletions EhTagClient/Record.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,21 @@

namespace EhTagClient
{
public sealed class AcceptableTranslationAttribute : ValidationAttribute
public sealed class AcceptableRecordAttribute : ValidationAttribute
{
public bool NoCheck { get; set; }

public override bool RequiresValidationContext => true;

protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (!(value is Record record))
return new ValidationResult($"The value '{value}' is not valid.");
var failedFields = new List<string>(2);
return new ValidationResult($"The value '{value}' is not valid.", new[] { validationContext.DisplayName });
record.Render(null);
if (NoCheck)
return ValidationResult.Success;
if (string.IsNullOrEmpty(record.Name.Text))
failedFields.Add(validationContext.MemberName + ".name.text");
if (failedFields.Count != 0)
return new ValidationResult($"Field should not be empty.", failedFields);
return new ValidationResult($"Field should not be empty.", new[] { validationContext.DisplayName + ".name" });
return ValidationResult.Success;
}
}
Expand All @@ -31,12 +33,12 @@ public sealed class AcceptableRawAttribute : ValidationAttribute
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (!(value is string raw))
return new ValidationResult($"The value '{value}' is not a valid tag.");
return new ValidationResult($"The value '{value}' is not a valid tag.", new[] { validationContext.DisplayName });
var traw = raw.Trim();
if (traw != raw)
return new ValidationResult($"The tag starts with or end with whitespaces.");
return new ValidationResult($"The tag starts with or end with whitespaces.", new[] { validationContext.DisplayName });
if (string.IsNullOrEmpty(traw))
return new ValidationResult($"The tag is too short.");
return new ValidationResult($"The tag is too short.", new[] { validationContext.DisplayName });
foreach (var ch in traw)
{
if ((ch >= 'a' && ch <= 'z')
Expand All @@ -46,7 +48,7 @@ protected override ValidationResult IsValid(object value, ValidationContext vali
|| ch == ' '
|| ch == '-')
continue;
return new ValidationResult($"The tag included non-alphanumeric characters which are not permitted. Only hyphens, periods, and spaces are allowed in tags.");
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 });
}
return ValidationResult.Success;
}
Expand Down

0 comments on commit bffdcab

Please sign in to comment.