Skip to content

Commit

Permalink
Adds Regular Expression Type (ChilliCream#3130)
Browse files Browse the repository at this point in the history
  • Loading branch information
joslat authored Mar 3, 2021
1 parent 2d942f6 commit 589c340
Show file tree
Hide file tree
Showing 16 changed files with 294 additions and 274 deletions.
12 changes: 6 additions & 6 deletions src/HotChocolate/Core/src/Types.Scalars/EmailAddressType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected override bool IsInstanceOfType(StringValueNode valueSyntax)
/// <inheritdoc />
protected override string ParseLiteral(StringValueNode valueSyntax)
{
if(!_validationRegex.IsMatch(valueSyntax.Value))
if (!_validationRegex.IsMatch(valueSyntax.Value))
{
throw ThrowHelper.EmailAddressType_ParseLiteral_IsInvalid(this);
}
Expand All @@ -64,7 +64,7 @@ protected override string ParseLiteral(StringValueNode valueSyntax)
/// <inheritdoc />
protected override StringValueNode ParseValue(string runtimeValue)
{
if(!_validationRegex.IsMatch(runtimeValue))
if (!_validationRegex.IsMatch(runtimeValue))
{
throw ThrowHelper.EmailAddressType_ParseValue_IsInvalid(this);
}
Expand All @@ -81,8 +81,8 @@ public override bool TrySerialize(object? runtimeValue, out object? resultValue)
return true;
}

if(runtimeValue is string s &&
_validationRegex.IsMatch(s))
if (runtimeValue is string s &&
_validationRegex.IsMatch(s))
{
resultValue = s;
return true;
Expand All @@ -101,8 +101,8 @@ public override bool TryDeserialize(object? resultValue, out object? runtimeValu
return true;
}

if(resultValue is string s &&
_validationRegex.IsMatch(s))
if (resultValue is string s &&
_validationRegex.IsMatch(s))
{
runtimeValue = s;
return true;
Expand Down
3 changes: 2 additions & 1 deletion src/HotChocolate/Core/src/Types.Scalars/HexColorType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
namespace HotChocolate.Types.Scalars
{
/// <summary>
/// The `HexColor` scalar type represents a valid HEX color code.
/// The `HexColor` scalar type represents a valid HEX color code as defined in
/// <a href="https://www.w3.org/TR/css-color-4/#hex-notation">W3 HEX notation</a>
/// </summary>
public class HexColorType : StringType
{
Expand Down
4 changes: 2 additions & 2 deletions src/HotChocolate/Core/src/Types.Scalars/HslType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
namespace HotChocolate.Types.Scalars
{
/// <summary>
/// The `HSL` scalar type represents a valid a CSS HSL color as defined
/// here https://developer.mozilla.org/en-US/docs/Web/CSS/color_value#hsl_colors.
/// The `HSL` scalar type represents a valid a CSS HSL color as defined in
/// <a href="https://www.w3.org/TR/css-color-3/#hsl-color">W3 HSL Colors</a>
/// </summary>
public class HslType : StringType
{
Expand Down
2 changes: 1 addition & 1 deletion src/HotChocolate/Core/src/Types.Scalars/HslaType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace HotChocolate.Types.Scalars
{
/// <summary>
/// The `HSLA` scalar type represents a valid a CSS HSLA color as defined
/// here https://developer.mozilla.org/en-US/docs/Web/CSS/color_value#hsl_colors.
/// in <a href="https://www.w3.org/TR/css-color-3/#hsla-color">W3 HSLA Color</a>
/// </summary>
public class HslaType : HslType
{
Expand Down
106 changes: 16 additions & 90 deletions src/HotChocolate/Core/src/Types.Scalars/IPv4Type.cs
Original file line number Diff line number Diff line change
@@ -1,114 +1,40 @@
using System;
using System.Text.RegularExpressions;
using HotChocolate.Language;

namespace HotChocolate.Types.Scalars
{
/// <summary>
/// The `IPv4` scalar type represents a valid a IPv4 address as defined
/// here https://en.wikipedia.org/wiki/IPv4.
/// The `IPv4` scalar type represents a valid a IPv4 address as defined in
/// <a href="https://tools.ietf.org/html/rfc791">RFC791</a>
/// </summary>
public class IPv4Type : StringType
public class IPv4Type : RegexType
{
private static readonly string _validationPattern =
ScalarResources.IPv4Type_ValidationPattern;

private static readonly Regex _validationRegex =
new(_validationPattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
private const string _validationPattern =
"(^(?:(?:(?:0?0?[0-9]|0?[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}(?:0?0?" +
"[0-9]|0?[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(?:\\/(?:[0-9]|[1-2][0-9]|3[0" +
"-2]))?)$)";

/// <summary>
/// Initializes a new instance of the <see cref="IPv4Type"/> class.
/// </summary>
public IPv4Type()
: this(
: base(
WellKnownScalarTypes.IPv4,
ScalarResources.IPv4Type_Description)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="IPv4Type"/> class.
/// </summary>
public IPv4Type(
NameString name,
string? description = null,
BindingBehavior bind = BindingBehavior.Explicit)
: base(name, description, bind)
_validationPattern,
ScalarResources.IPv4Type_Description,
RegexOptions.Compiled | RegexOptions.IgnoreCase)
{
Description = description;
}

/// <inheritdoc />
protected override bool IsInstanceOfType(string runtimeValue)
protected override Exception CreateParseLiteralError(StringValueNode valueSyntax)
{
return _validationRegex.IsMatch(runtimeValue);
return ThrowHelper.IPv4Type_ParseLiteral_IsInvalid(this);
}

/// <inheritdoc />
protected override bool IsInstanceOfType(StringValueNode valueSyntax)
protected override Exception CreateParseValueError(string runtimeValue)
{
return _validationRegex.IsMatch(valueSyntax.Value);
}

/// <inheritdoc />
protected override string ParseLiteral(StringValueNode valueSyntax)
{
if (!_validationRegex.IsMatch(valueSyntax.Value))
{
throw ThrowHelper.IPv4Type_ParseLiteral_IsInvalid(this);
}

return base.ParseLiteral(valueSyntax);
}

/// <inheritdoc />
protected override StringValueNode ParseValue(string runtimeValue)
{
if (!_validationRegex.IsMatch(runtimeValue))
{
throw ThrowHelper.IPv4Type_ParseValue_IsInvalid(this);
}

return base.ParseValue(runtimeValue);
}

/// <inheritdoc />
public override bool TrySerialize(object? runtimeValue, out object? resultValue)
{
if (runtimeValue is null)
{
resultValue = null;
return true;
}

if (runtimeValue is string s &&
_validationRegex.IsMatch(s))
{
resultValue = s;
return true;
}

resultValue = null;
return false;
}

/// <inheritdoc />
public override bool TryDeserialize(object? resultValue, out object? runtimeValue)
{
if (resultValue is null)
{
runtimeValue = null;
return true;
}

if (resultValue is string s &&
_validationRegex.IsMatch(s))
{
runtimeValue = s;
return true;
}

runtimeValue = null;
return false;
return ThrowHelper.IPv4Type_ParseValue_IsInvalid(this);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using HotChocolate.Language;

namespace HotChocolate.Types.Scalars
Expand Down
14 changes: 7 additions & 7 deletions src/HotChocolate/Core/src/Types.Scalars/PhoneNumberType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace HotChocolate.Types.Scalars
{
/// <summary>
/// The `PhoneNumber` scalar type scalar type represents a value that conforms to the standard
/// E.164 format as specified in: https://en.wikipedia.org/wiki/E.164.
/// E.164 format. <a href="https://en.wikipedia.org/wiki/E.164">See More</a>.
/// </summary>
public class PhoneNumberType : StringType
{
Expand Down Expand Up @@ -55,7 +55,7 @@ protected override bool IsInstanceOfType(StringValueNode valueSyntax)
/// <inheritdoc />
protected override string ParseLiteral(StringValueNode valueSyntax)
{
if(!_validationRegex.IsMatch(valueSyntax.Value))
if (!_validationRegex.IsMatch(valueSyntax.Value))
{
throw ThrowHelper.PhoneNumber_ParseLiteral_IsInvalid(this);
}
Expand All @@ -66,7 +66,7 @@ protected override string ParseLiteral(StringValueNode valueSyntax)
/// <inheritdoc />
protected override StringValueNode ParseValue(string runtimeValue)
{
if(!_validationRegex.IsMatch(runtimeValue))
if (!_validationRegex.IsMatch(runtimeValue))
{
throw ThrowHelper.PhoneNumber_ParseValue_IsInvalid(this);
}
Expand All @@ -83,8 +83,8 @@ public override bool TrySerialize(object? runtimeValue, out object? resultValue)
return true;
}

if(runtimeValue is string s &&
_validationRegex.IsMatch(s))
if (runtimeValue is string s &&
_validationRegex.IsMatch(s))
{
resultValue = s;
return true;
Expand All @@ -103,8 +103,8 @@ public override bool TryDeserialize(object? resultValue, out object? runtimeValu
return true;
}

if(resultValue is string s &&
_validationRegex.IsMatch(s))
if (resultValue is string s &&
_validationRegex.IsMatch(s))
{
runtimeValue = s;
return true;
Expand Down
Loading

0 comments on commit 589c340

Please sign in to comment.