@@ -6,7 +6,8 @@ public struct XStr: Val {
66 public let type : String
77 public let val : String
88
9- public init ( type: String , val: String ) {
9+ public init ( type: String , val: String ) throws {
10+ try type. validateXStrTypeName ( )
1011 self . type = type
1112 self . val = val
1213 }
@@ -38,8 +39,10 @@ extension XStr {
3839 )
3940 }
4041
41- self . type = try container. decode ( String . self, forKey: . type)
42- self . val = try container. decode ( String . self, forKey: . val)
42+ try self . init (
43+ type: container. decode ( String . self, forKey: . type) ,
44+ val: container. decode ( String . self, forKey: . val)
45+ )
4346 } else {
4447 throw DecodingError . typeMismatch (
4548 Self . self,
@@ -58,3 +61,26 @@ extension XStr {
5861 try container. encode ( val, forKey: . val)
5962 }
6063}
64+
65+ extension String {
66+ func validateXStrTypeName( ) throws {
67+ guard let firstChar = self . first else {
68+ throw XStrError . cannotBeEmptyString
69+ }
70+ guard firstChar. isUppercase else {
71+ throw XStrError . leadingCharacterIsNotUpperCase ( self )
72+ }
73+ for char in self {
74+ guard char. isTagChar else {
75+ throw XStrError . invalidCharacter ( char, self )
76+ }
77+ }
78+ }
79+ }
80+
81+ enum XStrError : Error {
82+ case cannotBeEmptyString
83+ case leadingCharacterIsNotUpperCase( String )
84+ case invalidCharacter( Character , String )
85+ }
86+
0 commit comments