Created
January 9, 2023 08:32
-
-
Save clemensv/4fef35f4d495e8d0d0bcd4576d19178a to your computer and use it in GitHub Desktop.
GPT wrote this
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
{%- set schema_name = "MySchema" | pascal -%} | |
{%- set list_type = "List" %} | |
namespace {{ schema_name }} | |
{ | |
static class ExceptionMessages | |
{ | |
public static readonly string ValueIsTooLow = "{0} value is too low. (min: {1})"; | |
public static readonly string ValueIsTooHigh = "{0} value is too high. (max: {1})"; | |
public static readonly string ValueIsTooLong = "{0} value is too long. (max length: {1})"; | |
public static readonly string ValueIsTooShort = "{0} value is too short. (min length: {1})"; | |
public static readonly string ValueIsTooBig = "{0} value is too big. (max: {1})"; | |
public static readonly string ValueIsTooSmall = "{0} value is too small. (min: {1})"; | |
public static readonly string ValueIsInvalid = "{0} value is invalid."; | |
public static readonly string ValueIsRequired = "{0} value is required."; | |
public static readonly string ValueIsNotUnique = "{0} value is not unique."; | |
public static readonly string ValueIsNotAnEmail = "{0} value is not an email."; | |
public static readonly string ValueIsNotAnUrl = "{0} value is not an URL."; | |
public static readonly string ValueIsNotADate = "{0} value is not a date."; | |
public static readonly string ValueIsNotADateTime = "{0} value is not a date time."; | |
public static readonly string ValueIsNotATime = "{0} value is not a time."; | |
} | |
static class RegexPatterns | |
{ | |
public static readonly string Email = @"^\S+@\S+$"; | |
public static readonly string Url = @"^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$"; | |
public static readonly string Date = @"^\d{4}-\d{2}-\d{2}$"; | |
public static readonly string DateTime = @"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(([+-]\d{2}:\d{2})|Z)?$"; | |
public static readonly string Time = @"^\d{2}:\d{2}:\d{2}$"; | |
} | |
{% for typeName in schema.definitions -%} | |
{%- set type = schema.definitions[typeName] -%} | |
{%- if type.type == 'object' -%} | |
{%- set baseType = None -%} | |
{%- if type.allOf -%} | |
{%- for allOfType in type.allOf -%} | |
{%- if allOfType.ref -%} | |
{%- set baseType = allOfType.ref.replace("#/definitions/", "") -%} | |
{%- endif -%} | |
{%- endfor -%} | |
{%- endif %} | |
public class {{ typeName | pascal }} | |
{ | |
{%- if baseType -%} | |
: {{ baseType | pascal }} | |
{%- endif %} | |
{%- for propertyName, property in type.properties.items() -%} | |
{%- set csharpType = "" -%} | |
{%- if property.type == 'integer' -%} | |
{%- if property.format == 'int32' -%} | |
{%- set csharpType = "int" -%} | |
{%- endif -%} | |
{%- if property.format == 'int64' -%} | |
{%- set csharpType = "long" -%} | |
{%- endif -%} | |
{%-elif property.type == 'number' -%} | |
{%- if property.format == 'float' -%} | |
{%- set csharpType = "float" -%} | |
{%- endif -%} | |
{%- if property.format == 'double' -%} | |
{%- set csharpType = "double" -%} | |
{%- endif -%} | |
{%- if property.format == 'decimal' -%} | |
{%- set csharpType = "decimal" -%} | |
{%- endif -%} | |
{%- elif property.type == 'string' -%} | |
{%- if property.format == 'date-time' -%} | |
{%- set csharpType = "DateTime" -%} | |
{%- endif -%} | |
{%- if property.format == 'email' -%} | |
{%- set csharpType = "string" -%} | |
{%- endif -%} | |
{%- if property.format == 'uri' -%} | |
{%- set csharpType = "Uri" -%} | |
{%- endif -%} | |
{%- if property.format == 'date' -%} | |
{%- set csharpType = "DateTime" -%} | |
{%- endif -%} | |
{%- if property.format == 'time' -%} | |
{%- set csharpType = "TimeSpan" -%} | |
{%- endif -%} | |
{%- if property.format == 'uuid' -%} | |
{%- set csharpType = "Guid" -%} | |
{%- endif -%} | |
{%- if property.format == 'ipv4' -%} | |
{%- set csharpType = "IPAddress" -%} | |
{%- endif -%} | |
{%- if property.format == 'ipv6' -%} | |
{%- set csharpType = "IPAddress" -%} | |
{%- endif -%} | |
{%- if property.format == 'hostname' -%} | |
{%- set csharpType = "string" -%} | |
{%- endif -%} | |
{%- if not property.format -%} | |
{%- set csharpType = "string" -%} | |
{%- endif -%} | |
{%- elif property.type == 'array' -%} | |
{%- if property.items.type == 'integer' -%} | |
{%- if property.items.format == 'int32' -%} | |
{%- set csharpType = list_type+"<int>" -%} | |
{%- endif -%} | |
{%- if property.items.format == 'int64' -%} | |
{%- set csharpType = list_type+"<long>" -%} | |
{%- endif -%} | |
{%- endif -%} | |
{%- if property.items.type == 'number' -%} | |
{%- if property.items.format == 'float' -%} | |
{%- set csharpType = list_type+"<float>" -%} | |
{%- endif -%} | |
{%- if property.items.format == 'double' -%} | |
{%- set csharpType = list_type+"<double>" -%} | |
{%- endif -%} | |
{%- if property.items.format == 'decimal' -%} | |
{%- set csharpType = list_type+"<decimal>" -%} | |
{%- endif -%} | |
{%- endif -%} | |
{%- if property.items.type == 'string' -%} | |
{%- if property.items.format == 'date-time' -%} | |
{%- set csharpType = list_type+"<DateTime>" -%} | |
{%- endif -%} | |
{%- if property.items.format == 'email' -%} | |
{%- set csharpType = list_type+"<string>" -%} | |
{%- endif -%} | |
{%- if property.items.format == 'uri' -%} | |
{%- set csharpType = list_type+"<Uri>" -%} | |
{%- endif -%} | |
{%- if property.items.format == 'uuid' -%} | |
{%- set csharpType = list_type+"<Guid>" -%} | |
{%- endif -%} | |
{%- if property.items.format == 'ipv4' -%} | |
{%- set csharpType = list_type+"<IPAddress>" -%} | |
{%- endif -%} | |
{%- if property.items.format == 'ipv6' -%} | |
{%- set csharpType = list_type+"<IPAddress>" -%} | |
{%- endif -%} | |
{%- if property.items.format == 'hostname' -%} | |
{%- set csharpType = list_type+"<string>" -%} | |
{%- endif -%} | |
{%- if not property.items.format -%} | |
{%- set csharpType = list_type+"<string>" -%} | |
{%- endif -%} | |
{%- endif -%} | |
{%- if property.items['$ref'] -%} | |
{%- set ref = property.items['$ref'] -%} | |
{%- set ref = ref.split('/')[-1] -%} | |
{%- set csharpType = ref -%} | |
{%- endif -%} | |
{%- else -%} | |
{%- set csharpType = "object" -%} | |
{%- endif -%} | |
{%- if not property.type and not property.items -%} | |
{%- if property['$ref'] -%} | |
{%- set ref = property['$ref'] -%} | |
{%- set ref = ref.split('/')[-1] -%} | |
{%- set csharpType = ref -%} | |
{%- endif -%} | |
{%- endif -%} | |
{%- if property.enum -%} | |
public enum {{ propertyName | pascal }}Enum | |
{ | |
{%- for value in property.enum -%} | |
{{ value | pascal }} = {{ loop.index - 1 }}, | |
{%- endfor -%} | |
} | |
private {{ propertyName | pascal }}Enum _{{ propertyName }} = {{ propertyName | pascal }}Enum.{{ property.enum[0] | pascal }}; | |
public {{ propertyName | pascal }}Enum {{ propertyName | pascal }} | |
{ | |
get | |
{ | |
return _{{ propertyName }}; | |
} | |
set | |
{ | |
if (!Enum.IsDefined(typeof({{ propertyName | pascal }}Enum), value)) | |
{ | |
throw new ArgumentOutOfRangeException("{{ propertyName | pascal }}", ExceptionMessages.ValueIsInvalid); | |
} | |
_{{ propertyName }} = value; | |
} | |
} | |
{%- else %} | |
private {{ csharpType }} _{{ propertyName }} { get; set; } | |
{% if property.description -%} | |
/// <description>{{property.description}}</description> | |
{%- endif %} | |
[System.Text.Json.Serialization.JsonPropertyName("{{ propertyName }}")] | |
public {{ csharpType }} {{ propertyName | pascal }} | |
{ | |
get | |
{ | |
return _{{ propertyName }}; | |
} | |
set | |
{ | |
{%- if property.minimum -%} | |
if (value < {{ property.minimum }}) | |
{ | |
throw new ArgumentOutOfRangeException("{{ propertyName | pascal }}", string.Format(ExceptionMessages.ValueIsTooSmall, value, {{ property.minimum }})); | |
} | |
{%- endif -%} | |
{%- if property.exclusiveMinimum -%} | |
if (value <= {{ property.exclusiveMinimum }}) | |
{ | |
throw new ArgumentOutOfRangeException("{{ propertyName | pascal }}", string.Format(ExceptionMessages.ValueIsTooSmall, value, {{ property.exclusiveMinimum }})); | |
} | |
{%- endif -%} | |
{%- if property.maximum -%} | |
if (value > {{ property.maximum }}) | |
{ | |
throw new ArgumentOutOfRangeException("{{ propertyName | pascal }}", string.Format(ExceptionMessages.ValueIsTooBig, value, {{ property.maximum }})); | |
} | |
{%- endif -%} | |
{%- if property.exclusiveMaximum -%} | |
if (value >= {{ property.exclusiveMaximum }}) | |
{ | |
throw new ArgumentOutOfRangeException("{{ propertyName | pascal }}", string.Format(ExceptionMessages.ValueIsTooBig, value, {{ property.exclusiveMaximum }})); | |
} | |
{%- endif -%} | |
{%- if property.maxLength -%} | |
if (value.ToString().Length > {{ property.maxLength }}) | |
{ | |
throw new ArgumentOutOfRangeException("{{ propertyName | pascal }}", string.Format(ExceptionMessages.ValueIsTooLong, value, {{ property.maxLength }})); | |
} | |
{%- endif -%} | |
{%- if property.minLength -%} | |
if (value.ToString().Length < {{ property.minLength }}) | |
{ | |
throw new ArgumentOutOfRangeException("{{ propertyName | pascal }}", string.Format(ExceptionMessages.ValueIsTooShort, value, {{ property.minLength }})); | |
} | |
{%- endif -%} | |
{%- if property.pattern -%} | |
if (!System.Text.RegularExpressions.Regex.IsMatch(value.ToString(), Patterns.{{ property.pattern | pascal }})) | |
{ | |
throw new ArgumentOutOfRangeException("{{ propertyName | pascal }}", ExceptionMessages.ValueIsInvalid); | |
} | |
{%- endif -%} | |
{%- if property.format == "email" -%} | |
if (!System.Text.RegularExpressions.Regex.IsMatch(System.Text.Json.JsonSerializer.ToJson(value), Patterns.Email, System.Text.RegularExpressions.RegexOptions.Singleline)) | |
{ | |
throw new System.ArgumentException(ExceptionMessages.ValueIsNotAnEmail, "{{ propertyName | pascal }}"); | |
} | |
{%- endif -%} | |
{%- if property.format == "uri" -%} | |
if (!System.Uri.IsWellFormedUriString(System.Text.Json.JsonSerializer.ToJson(value), System.UriKind.RelativeOrAbsolute)) | |
{ | |
throw new System.ArgumentException(ExceptionMessages.ValueIsNotAnUrl, "{{ propertyName | pascal }}"); | |
} | |
{%- endif -%} | |
{%- if property.format == "date" -%} | |
if (!System.DateTime.TryParseExact(System.Text.Json.JsonSerializer.ToJson(value), Formats.Date, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.RoundtripKind, out _)) | |
{ | |
throw new System.ArgumentException(ExceptionMessages.ValueIsNotADate, "{{ propertyName | pascal }}"); | |
} | |
{%- endif -%} | |
{%- if property.format == "date-time" -%} | |
if (!System.DateTime.TryParseExact(System.Text.Json.JsonSerializer.ToJson(value), Formats.DateTime, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.RoundtripKind, out _)) | |
{ | |
throw new System.ArgumentException(ExceptionMessages.ValueIsNotADateTime, "{{ propertyName | pascal }}"); | |
} | |
{%- endif -%} | |
{%- if property.format == "time" -%} | |
if (!System.DateTime.TryParseExact(System.Text.Json.JsonSerializer.ToJson(value), Formats.Time, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.RoundtripKind, out _)) | |
{ | |
throw new System.ArgumentException(ExceptionMessages.ValueIsNotATime, "{{ propertyName | pascal }}"); | |
} | |
{%- endif %} | |
_{{ propertyName }} = value; | |
} | |
} | |
{% endif -%} | |
{%- endfor -%} | |
{% endif -%} | |
} | |
{%- endfor %} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment