datavalidator
2.0.2

Datavalidator is designed to be a simple data validation library.
Support: [email protected]
$ boss install github.com/dliocode/datavalidatorAdd the following folders to your project, in Project> Options> Delphi Compiler> Search Path
../datavalidator/src/core
../datavalidator/src/sanitializators
../datavalidator/src/validators
uses DataValidator;
var
LResult: IDataValidatorResult;
begin
LResult :=
TDataValidator.Values
.Validate('APELIDO')
.Trim
.&Not.IsEmpty.WithMessage('Você não informou o seu apelido!')
.IsAlpha(TDataValidatorLocaleLanguage.tl_pt_BR).WithMessage('Seu apelido deve conter apenas letras!')
.IsLength(1, 10).WithMessage('O apelido deve ter no máximo 10 caracteres!')
.&End
.Validate('[email protected]')
.Trim
.&Not.IsEmpty.WithMessage('Você não informou o seu e-mail!')
.IsEmail.WithMessage('Não é um e-mail válido!')
.NormalizeEmail
.&End
.Check;
var
LJO: TJSONObject;
LResult: IDataValidatorResult;
begin
LJO := TJSONObject.ParseJSONValue('{"apelido":"DLIO", "email":"[email protected]"}') as TJSONObject;
LResult :=
TDataValidator.JSON(AJO)
.Validate('apelido')
.Value
.Trim
.&Not.IsEmpty.WithMessage('Você não informou o seu apelido!')
.IsAlpha(TDataValidatorLocaleLanguage.tl_pt_BR).WithMessage('Seu apelido deve conter apenas letras!')
.IsLength(1, 10).WithMessage('O apelido deve ter no máximo 10 caracteres!')
.&End
.&End
.Validate('email')
.Value
.Trim
.&Not.IsEmpty.WithMessage('Você não informou o seu e-mail!')
.IsEmail.WithMessage('Não é um e-mail válido!')
.NormalizeEmail
.&End
.&End
.Validate('login')
.Key // Faz a validação somente da key
.IsRequired.WithMessage('É obrigatório ter a Key "login" no JSON.')
.&End
.Value // Faz a validação somente do valor dentro da Key 'login'
.Trim
.&Not.IsEmpty.WithMessage('Você não informou o login!')
.IsAlpha(TDataValidatorLocaleLanguage.tl_pt_BR).WithMessage('Seu login deve conter apenas letras!')
.IsLength(1, 10).WithMessage('O login deve ter no máximo 10 caracteres!')
.&End
.&End
.Validate('nome')
.Key
.IsOptionalKey // É opcional - se existir a Key "nome" ele faz a validação
.&End
.Value
.Trim
.&Not.IsEmpty.WithMessage('Você não informou o nome!')
.IsAlpha(TDataValidatorLocaleLanguage.tl_pt_BR).WithMessage('Seu nome deve conter apenas letras!')
.IsLength(1, 10).WithMessage('O nome deve ter no máximo 10 caracteres!')
.&End
.&End
.CheckAll;
Check : Check all the validate , but if there is a false return item it interrupts, returning a single message with error if you have!
Checkall : Check all the validate , returning all messages with error if you have!
Using the CheckAll(TDataValidatorCheckAll.tcFirst) it returns only the first error message from each validate .
Result := LResult.Ok; // True = Nenhum erro | False = Tem erro
Result := LResult.Informations.Message;
Result := LResult.Informations.GetItem(0).Message;
Result := LResult.Informations.Count;
Result := LResult.Values[0]; // Recupera o primeiro valor
function SchemaNome(const AField: string): IDataValidatorSchemaContext;
begin
Result :=
TDataValidator.Schema
.Validate
.Trim
.&Not.IsEmpty.WithMessage('Preencha o campo %s !', [AField]) // Não pode ser vazio
.IsLength(2, 10).WithMessage('O campo %s deve conter entre 2 a 10 caracteres!', [AField])
.IsAlpha(TDataValidatorLocaleLanguage.tl_pt_BR).WithMessage('O campo %s possui caracteres inválidos!', [AField])
.&End;
end;
function SchemaEmail(const AField: string): IDataValidatorSchemaContext;
begin
Result :=
TDataValidator.Schema
.Validate
.Trim
.&Not.IsEmpty.WithMessage('O %s não pode ser vazio!',[AField])
.IsLength(2, 999).WithMessage('O campo %s deve ter mais de 6 caracteres!', [AField])
.IsEmail.WithMessage('Não é um %s válido!',[AField])
.NormalizeEmail
.&End;
end;
begin
TDataValidator.Values
.Validate(ANome)
.AddSchema(SchemaNome('Nome'))
.&End
.Validate(AApelido)
.AddSchema(SchemaNome('Apelido'))
.IsUppercase.WithMessage('O apelido (${value}) deve ser digitado tudo em maiúscula!') // Add outra validação
.&End
.Validate(AEmail)
.AddSchema(SchemaEmail('E-mail'))
.&End
.Validate(AEmailConfirmacao)
.AddSchema(SchemaEmail('E-mail de confirmação'))
.&End
.CheckAll;
end.
Sanitizer is the treatment of a value.
Example: Sanitizers MD5: Take the validate value and convert to MD5
TDataValidator.Values
.Validate('APELIDO')
.ToMD5
.&End
| Name | Information |
|---|---|
| Not | When used this validator, it denies the validator result. Ex: Validate ('email'). & Not.isempty It is understood that the value should not be empty. |
| Run | Defines what should be performed if that validate does not pass the validation. The execution of this procedure is manual form. Ex: Checkall.informations.getitem (0) .onexecute; |
| Withmessage | Defines the error message. To add the validated value to the message, just add the $ {value} tag. Ex: Isemail.withmessage ('email $ {value} is invalid!') . output: E-mail null@@null está inválido! |
| Validation for values | VALIDATION JSON (Key/VALUE) | Sanitizers for VALUES |
|---|---|---|
| Customvalue | (Key) | Customizer |
| Contains | (Key) Isrequired | NormalizeeEmail |
| Endswith | (VALUE) CUSTOMJSONVALUE | Onlynumbers |
| Isalpha | (VALUE) ISJSJONNULL | REMOVEACCCENTS |
| Isalphanumeric | (VALUE) ISJSONBOOLEAN | Replace |
| ISacii | (VALUE) ISJSONNUMERIC | Tobase64decode |
| ISBASE32 | (VALUE) ISJSONSTRING | Tobase64Encode |
| ISBASE58 | (VALUE) MINEMTES | All |
| ISBASE64 | (VALUE) MAXITES | All -atetime |
| Isbetween | (VALUE) + VALIDATION FOR VALUES | TOHTMLDECODE |
| Isboolean | Tohtmlencode | |
| ISBTCADDRESS | Tointeger | |
| ISCNPJ | Tolowercase | |
| ISCPF | Tomd5 | |
| ISCPFCNPJ | Tonumeric | |
| Isdate | Totime | |
| ISDATEBETWEEN | Touppercase | |
| ISDATEQUALS | Tourldecode | |
| ISDATEGRETETHAN | Tourlencode | |
| ISDATELESTHAN | Trim | |
| Isdatetime | Trimleft | |
| Ismail | Trimright | |
| ISMPTY | ||
| ISEQUALS | ||
| Isethereumaddress | ||
| Isgaterthan | ||
| Isgtin | ||
| ISGTIN8 | ||
| ISGTIN12 | ||
| ISGTIN13 | ||
| ISGTIN14 | ||
| Ishexadecimal | ||
| Ishexcolor | ||
| Isinteger | ||
| Isip | ||
| Isipv4 | ||
| Isipv6 | ||
| Isiso8601 | ||
| Isjson | ||
| Isjsonarray | ||
| ISJSONOBJECT | ||
| ISJWT | ||
| Islatlong | ||
| Islength | ||
| Islesssthan | ||
| Islocale | ||
| Islowercase | ||
| Ismacaddress | ||
| Ismagneturi | ||
| ISMD5 | ||
| Ismimetype | ||
| Ismongoid | ||
| Isnegative | ||
| Isnumeric | ||
| Isootal | ||
| Isocal | ||
| ISPASSPORTNUMBER | ||
| Isphonenumber | ||
| Isport | ||
| Ispositive | ||
| Ispostalcode | ||
| ISRGBColor | ||
| ISSSN | ||
| Istime | ||
| ISTIMEBETWEEN | ||
| Istimeequals | ||
| ISTIMEGREATETHAN | ||
| ISTIMELESTHAN | ||
| Isuppercase | ||
| Isurn | ||
| Isuuid | ||
| Isuuidv1 | ||
| ISUUIDV2 | ||
| ISUUIDV3 | ||
| Isuuidv4 | ||
| ISUUIDV5 | ||
| Iszero | ||
| REGEXISMATCH | ||
| Startswith |