forked from ChilliCream/graphql-platform
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds Regular Expression Type (ChilliCream#3130)
- Loading branch information
Showing
16 changed files
with
294 additions
and
274 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
1 change: 0 additions & 1 deletion
1
src/HotChocolate/Core/src/Types.Scalars/NonNegativeFloatType.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
using System; | ||
using HotChocolate.Language; | ||
|
||
namespace HotChocolate.Types.Scalars | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.