Skip to content

Commit c1cde05

Browse files
committed
Ajustes gerais
* Ajuste da validação: - IsBase64 * Adicionado nova validação: - IsJWT - IsGTIN - IsGTIN8 - IsGTIN12 - IsGTIN13 - IsGTIN14
1 parent 38d89fb commit c1cde05

8 files changed

+298
-16
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,12 +279,18 @@ TDataValidator.Values
279279
|IsEquals | | |
280280
|IsEthereumAddress | | |
281281
|IsGreaterThan | | |
282+
|IsGTIN | | |
283+
|IsGTIN8 | | |
284+
|IsGTIN12 | | |
285+
|IsGTIN13 | | |
286+
|IsGTIN14 | | |
282287
|IsHexadecimal | | |
283288
|IsHexColor | | |
284289
|IsInteger | | |
285290
|IsIP | | |
286291
|IsISO8601 | | |
287292
|IsJSON | | |
293+
|IsJWT | | |
288294
|IsLength | | |
289295
|IsLessThan | | |
290296
|IsLocale | | |

src/core/DataValidator.Context.Intf.pas

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,18 @@ interface
8989
function IsEquals(const AValueEquals: TArray<string>; const ACaseSensitive: Boolean = False): T; overload;
9090
function IsEthereumAddress(): T;
9191
function IsGreaterThan(const AValueGreaterThan: Integer): T;
92+
function IsGTIN(): T;
93+
function IsGTIN8(): T;
94+
function IsGTIN12(): T;
95+
function IsGTIN13(): T;
96+
function IsGTIN14(): T;
9297
function IsHexadecimal(): T;
9398
function IsHexColor(): T;
9499
function IsInteger(): T;
95100
function IsIP(): T;
96101
function IsISO8601(): T;
97102
function IsJSON(): T;
103+
function IsJWT(): T;
98104
function IsLength(const AMin: Integer; const AMax: Integer): T;
99105
function IsLessThan(const AValueLessThan: Integer): T;
100106
function IsLocale(): T;

src/core/DataValidator.Context.pas

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,18 @@ TDataValidatorContext<T: IInterface> = class(TInterfacedObject, IDataValidator
6666
function IsEquals(const AValueEquals: string; const ACaseSensitive: Boolean = False): T; overload;
6767
function IsEthereumAddress(): T;
6868
function IsGreaterThan(const AValueGreaterThan: Integer): T;
69+
function IsGTIN(): T;
70+
function IsGTIN8(): T;
71+
function IsGTIN12(): T;
72+
function IsGTIN13(): T;
73+
function IsGTIN14(): T;
6974
function IsHexadecimal(): T;
7075
function IsHexColor(): T;
7176
function IsInteger(): T;
7277
function IsIP(): T;
7378
function IsISO8601(): T;
7479
function IsJSON(): T;
80+
function IsJWT(): T;
7581
function IsLength(const AMin: Integer; const AMax: Integer): T;
7682
function IsLessThan(const AValueLessThan: Integer): T;
7783
function IsLocale(): T;
@@ -198,12 +204,14 @@ implementation
198204
Validator.IsEthereumAddress,
199205
Validator.IsPhoneNumber,
200206
Validator.IsGreaterThan,
207+
Validator.IsGTIN,
201208
Validator.IsHexadecimal,
202209
Validator.IsHexColor,
203210
Validator.IsInteger,
204211
Validator.IsIP,
205212
Validator.IsISO8601,
206213
Validator.IsJSON,
214+
Validator.IsJWT,
207215
Validator.IsLength,
208216
Validator.IsLessThan,
209217
Validator.IsLocale,
@@ -471,6 +479,31 @@ function TDataValidatorContext<T>.IsGreaterThan(const AValueGreaterThan: Integer
471479
Result := Add(TValidatorIsGreaterThan.Create(AValueGreaterThan, Format('Value is not greate than %d!', [AValueGreaterThan])));
472480
end;
473481

482+
function TDataValidatorContext<T>.IsGTIN: T;
483+
begin
484+
Result := Add(TValidatorIsGTIN.Create(TTypeGTIN.tgAll, 'Value is not GTIN (Global Trade Item Number)!'));
485+
end;
486+
487+
function TDataValidatorContext<T>.IsGTIN8: T;
488+
begin
489+
Result := Add(TValidatorIsGTIN.Create(TTypeGTIN.tg8, 'Value is not GTIN-8 (Global Trade Item Number)!'));
490+
end;
491+
492+
function TDataValidatorContext<T>.IsGTIN12: T;
493+
begin
494+
Result := Add(TValidatorIsGTIN.Create(TTypeGTIN.tg12, 'Value is not GTIN-12 (Global Trade Item Number)!'));
495+
end;
496+
497+
function TDataValidatorContext<T>.IsGTIN13: T;
498+
begin
499+
Result := Add(TValidatorIsGTIN.Create(TTypeGTIN.tg13, 'Value is not GTIN-13 (Global Trade Item Number)!'));
500+
end;
501+
502+
function TDataValidatorContext<T>.IsGTIN14: T;
503+
begin
504+
Result := Add(TValidatorIsGTIN.Create(TTypeGTIN.tg14, 'Value is not GTIN-14 (Global Trade Item Number)!'));
505+
end;
506+
474507
function TDataValidatorContext<T>.IsHexadecimal: T;
475508
begin
476509
Result := Add(TValidatorIsHexadecimal.Create('Value is not hexadecimal!'));
@@ -501,6 +534,11 @@ function TDataValidatorContext<T>.IsJSON: T;
501534
Result := Add(TValidatorIsJSON.Create('Value is not JSON (JavaScript Object Notation)!'));
502535
end;
503536

537+
function TDataValidatorContext<T>.IsJWT: T;
538+
begin
539+
Result := Add(TValidatorIsJWT.Create('Value is not JWT (JSON Web Token)!'));
540+
end;
541+
504542
function TDataValidatorContext<T>.IsLength(const AMin, AMax: Integer): T;
505543
begin
506544
Result := Add(TValidatorIsLength.Create(AMin, AMax, Format('Value required length min(%d) and max(%d)!', [AMin, AMax])));
@@ -623,32 +661,32 @@ function TDataValidatorContext<T>.IsURL: T;
623661

624662
function TDataValidatorContext<T>.IsUUID: T;
625663
begin
626-
Result := Add(TValidatorIsUUID.Create(TUUIDVersion.tuAll, 'Value is not UUID (Universally Unique Identifier)!'));
664+
Result := Add(TValidatorIsUUID.Create(TTypeUUID.tuAll, 'Value is not UUID (Universally Unique Identifier)!'));
627665
end;
628666

629667
function TDataValidatorContext<T>.IsUUIDv1: T;
630668
begin
631-
Result := Add(TValidatorIsUUID.Create(TUUIDVersion.tuV1, 'Value is not UUID (Universally Unique Identifier) v1!'));
669+
Result := Add(TValidatorIsUUID.Create(TTypeUUID.tuV1, 'Value is not UUIDv1 (Universally Unique Identifier)!'));
632670
end;
633671

634672
function TDataValidatorContext<T>.IsUUIDv2: T;
635673
begin
636-
Result := Add(TValidatorIsUUID.Create(TUUIDVersion.tuV2, 'Value is not UUID (Universally Unique Identifier) v2!'));
674+
Result := Add(TValidatorIsUUID.Create(TTypeUUID.tuV2, 'Value is not UUIDv2 (Universally Unique Identifier)!'));
637675
end;
638676

639677
function TDataValidatorContext<T>.IsUUIDv3: T;
640678
begin
641-
Result := Add(TValidatorIsUUID.Create(TUUIDVersion.tuV3, 'Value is not UUID (Universally Unique Identifier) v3!'));
679+
Result := Add(TValidatorIsUUID.Create(TTypeUUID.tuV3, 'Value is not UUIDv3 (Universally Unique Identifier)!'));
642680
end;
643681

644682
function TDataValidatorContext<T>.IsUUIDv4: T;
645683
begin
646-
Result := Add(TValidatorIsUUID.Create(TUUIDVersion.tuV4, 'Value is not UUID (Universally Unique Identifier) v4!'));
684+
Result := Add(TValidatorIsUUID.Create(TTypeUUID.tuV4, 'Value is not UUIDv4 (Universally Unique Identifier)!'));
647685
end;
648686

649687
function TDataValidatorContext<T>.IsUUIDv5: T;
650688
begin
651-
Result := Add(TValidatorIsUUID.Create(TUUIDVersion.tuV5, 'Value is not UUID (Universally Unique Identifier) v5!'));
689+
Result := Add(TValidatorIsUUID.Create(TTypeUUID.tuV5, 'Value is not UUIDv5 (Universally Unique Identifier)!'));
652690
end;
653691

654692
function TDataValidatorContext<T>.IsZero: T;

src/core/DataValidator.JSON.Context.pas

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,13 @@ function TDataValidatorJSONContext<T>.CustomJSONValue(const AExecute: TDataValid
125125
function TDataValidatorJSONContext<T>.IsArray: IDataValidatorJSONContextValue<T>;
126126
begin
127127
Result := Self;
128-
Add(TDataValidatorJSONValueIsArray.Create('Value not is JSONArray!'));
128+
Add(TDataValidatorJSONValueIsArray.Create('Value not is JSON Array!'));
129129
end;
130130

131131
function TDataValidatorJSONContext<T>.IsObject: IDataValidatorJSONContextValue<T>;
132132
begin
133133
Result := Self;
134-
Add(TDataValidatorJSONValueIsObject.Create('Value not is JSONObject!'));
134+
Add(TDataValidatorJSONValueIsObject.Create('Value not is JSON Object!'));
135135
end;
136136

137137
function TDataValidatorJSONContext<T>.MinItems(const AMinItems: Integer): IDataValidatorJSONContextValue<T>;

src/validators/Validator.IsBase64.pas

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ interface
1111

1212
uses
1313
DataValidator.ItemBase,
14-
System.SysUtils, System.RegularExpressions;
14+
System.SysUtils, System.RegularExpressions, System.NetEncoding;
1515

1616
type
1717
TValidatorIsBase64 = class(TDataValidatorItemBase, IDataValidatorItem)
@@ -40,8 +40,16 @@ function TValidatorIsBase64.Checked: IDataValidatorResult;
4040
R := False;
4141

4242
if not Trim(LValue).IsEmpty then
43-
if (Length(LValue) mod 4) <> 0 then
44-
R := TRegEx.IsMatch(LValue, '^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$');
43+
begin
44+
R := TRegEx.IsMatch(LValue, '^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$');
45+
46+
if not R then
47+
try
48+
LValue := TNetEncoding.Base64.Decode(LValue);
49+
R := True;
50+
except
51+
end;
52+
end;
4553

4654
if FIsNot then
4755
R := not R;

src/validators/Validator.IsGTIN.pas

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
{
2+
*************************************
3+
Created by Danilo Lucas
4+
Github - https://github.com/dliocode
5+
*************************************
6+
}
7+
8+
unit Validator.IsGTIN; // GTIN (Global Trade Item Number)
9+
10+
interface
11+
12+
uses
13+
DataValidator.ItemBase,
14+
System.SysUtils, System.RegularExpressions;
15+
16+
type
17+
TTypeGTIN = (tgAll, tg8, tg12, tg13, tg14);
18+
19+
TValidatorIsGTIN = class(TDataValidatorItemBase, IDataValidatorItem)
20+
private
21+
FType: TTypeGTIN;
22+
function GetPattern: string;
23+
function CalculateCheckDigit(const AValue: string): Integer;
24+
public
25+
function Checked: IDataValidatorResult;
26+
constructor Create(const AGTINType: TTypeGTIN; const AMessage: string; const AExecute: TDataValidatorInformationExecute = nil);
27+
end;
28+
29+
implementation
30+
31+
{ TValidatorIsGTIN }
32+
33+
constructor TValidatorIsGTIN.Create(const AGTINType: TTypeGTIN; const AMessage: string; const AExecute: TDataValidatorInformationExecute = nil);
34+
begin
35+
FType := AGTINType;
36+
FMessage := AMessage;
37+
FExecute := AExecute;
38+
end;
39+
40+
function TValidatorIsGTIN.Checked: IDataValidatorResult;
41+
var
42+
LValue: string;
43+
R: Boolean;
44+
LDigit: Integer;
45+
begin
46+
LValue := GetValueAsString;
47+
R := False;
48+
49+
if not Trim(LValue).IsEmpty then
50+
if TRegEx.IsMatch(LValue, GetPattern) then
51+
begin
52+
LDigit := CalculateCheckDigit(LValue);
53+
R := LDigit = StrToInt(LValue[Length(LValue)]);
54+
end;
55+
56+
if FIsNot then
57+
R := not R;
58+
59+
Result := TDataValidatorResult.Create(R, TDataValidatorInformation.Create(LValue, FMessage, FExecute));
60+
end;
61+
62+
function TValidatorIsGTIN.GetPattern: string;
63+
begin
64+
case FType of
65+
tgAll:
66+
Result := '^(\d{8}|\d{12,14})$';
67+
tg8:
68+
Result := '^\d{8}$';
69+
tg12:
70+
Result := '^\d{12}$';
71+
tg13:
72+
Result := '^\d{13}$';
73+
tg14:
74+
Result := '^\d{14}$';
75+
end;
76+
end;
77+
78+
function TValidatorIsGTIN.CalculateCheckDigit(const AValue: string): Integer;
79+
var
80+
I: Integer;
81+
LSum: Integer;
82+
LBase10: Integer;
83+
begin
84+
// https://en.wikipedia.org/wiki/International_Article_Number#Calculation_of_checksum_digit
85+
86+
LSum := 0;
87+
88+
for I := 1 to Pred(Length(AValue)) do
89+
if Length(AValue) = 13 then
90+
begin
91+
if Odd(I) then
92+
LSum := LSum + StrToInt(AValue[I])
93+
else
94+
LSum := LSum + (StrToInt(AValue[I]) * 3);
95+
end
96+
else
97+
begin
98+
if Odd(I) then
99+
LSum := LSum + StrToInt(AValue[I]) * 3
100+
else
101+
LSum := LSum + StrToInt(AValue[I]);
102+
end;
103+
104+
LBase10 := LSum;
105+
106+
if not(LBase10 mod 10 = 0) then
107+
while not(LBase10 mod 10 = 0) do
108+
LBase10 := LBase10 + 1;
109+
110+
Result := LBase10 - LSum;
111+
end;
112+
113+
end.

0 commit comments

Comments
 (0)