#1 2015-03-23 14:22:39

ws
Member
Registered: 2015-03-17
Posts: 19

Restfull Validation

how to validate a field that is empty in app Restfull

Offline

#2 2015-03-23 16:33:32

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,272
Website

Offline

#3 2015-03-24 23:02:42

ws
Member
Registered: 2015-03-17
Posts: 19

Re: Restfull Validation

even putting this check is writing to the database, presenting no error.

function DataModel: TSQLModel;
begin
  result := TSQLModel.Create([TPersona],SERVER_ROOT);
  TPersona.AddFilterOrValidate('Name',TSynValidateText.Create(''));
end;

how can I use {"minlength": 5, "MaxLength": 10} in TSynValidateText, which syntax.

Last edited by ws (2015-03-25 00:00:41)

Offline

#4 2015-03-25 01:02:37

ws
Member
Registered: 2015-03-17
Posts: 19

Re: Restfull Validation

I found this code :

SQLRecordClasses[iClasses].AddFilterOrValidate(List^[iProps]^.Name,
            TSynValidateText.Create(Format('{"MinLength":0,"MaxLength":%d}', [list^[iProps]^.Index])));

Offline

#5 2015-03-25 01:44:30

ws
Member
Registered: 2015-03-17
Posts: 19

Re: Restfull Validation

I tried this code, also did not work, was recorded in the database with two characters

function DataModel: TSQLModel;
begin
  result := TSQLModel.Create([TPersona],SERVER_ROOT);
  TPersona.AddFilterOrValidate('Name',TSynValidateText.Create('{"minlength": 5, "MaxLength": 10}'));
end;

Offline

#6 2015-03-25 06:07:27

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,272
Website

Re: Restfull Validation

You have to launch the validation before writing, as stated by the documentation.

Offline

#7 2015-04-01 17:18:34

ws
Member
Registered: 2015-03-17
Posts: 19

Re: Restfull Validation

returning to the tests.

I used the Validate method of TSQLRecord class, doing override, code below:

   ...

  TPersona = class(TSQLRecord)
  private
    fName: RawUTF8;
    fEndereco: RawUTF8;
  public
    function Validate(aRest: TSQLRest; const aFields: TSQLFieldBits=[0..MAX_SQLFIELDS-1];
      aInvalidFieldIndex: PInteger=nil; aValidator: PSynValidate=nil): string; override;
  published

  ...

function TPersona.Validate(aRest: TSQLRest; const aFields: TSQLFieldBits=[0..MAX_SQLFIELDS-1];
      aInvalidFieldIndex: PInteger=nil; aValidator: PSynValidate=nil): string;
var texto :String;
begin
  texto := '{"error":"only test"}';
  result := UTF8ToString(texto);
end;

at what time the validate method is called, I created a new record and did not return the message. 

how to call the validate function.

Last edited by ws (2015-04-01 19:04:42)

Offline

#8 2015-04-01 21:54:32

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,272
Website

Re: Restfull Validation

aPersona.Validate ?

Offline

#9 2015-04-02 14:56:27

ws
Member
Registered: 2015-03-17
Posts: 19

Re: Restfull Validation

I understand that I have to do to override TRecord.Validate class, as well I did. In fact I intend to do a review of the field "name" persona table, it not accepted short description.

I'm using as a basis the project "30 - MVC Server" -> MVCServer.dpr

The following model code.   What else to do?

 unit RESTModel;

interface

uses
  SynCommons,
  mORMot;

type
  TPersona = class(TSQLRecord) // TSQLRecord has already ID: integer primary key
  private
    fName: RawUTF8;
  public
    function Validate(aRest: TSQLRest; const aFields: TSQLFieldBits=[0..MAX_SQLFIELDS-1];
      aInvalidFieldIndex: PInteger=nil; aValidator: PSynValidate=nil): string; override;
  published
    /// ORM will create a NAME VARCHAR(80) column
    property Name:     RawUTF8 index 80 read fName     write fName;
  protected
  end;

function DataModel: TSQLModel;

const
  SERVER_ROOT = 'root';
  SERVER_PORT = '888';

implementation

function TPersona.Validate(aRest: TSQLRest; const aFields: TSQLFieldBits=[0..MAX_SQLFIELDS-1];
      aInvalidFieldIndex: PInteger=nil; aValidator: PSynValidate=nil): string;
var text :String;
begin
  text := '';
  // pseudo code
  if length(name)<=5 then
    text := '{"error":"should be above 5 characters"}';
  result := UTF8ToString(text);
end;

function DataModel: TSQLModel;
begin
  result := TSQLModel.Create([TPersona],SERVER_ROOT);
  
  TPersona.AddFilterOrValidate('Name',TSynValidateText.Create('{"minlength": 5}'));
end;


end.

Last edited by ws (2015-04-02 14:57:24)

Offline

#10 2015-04-02 15:41:42

warleyalex
Member
From: Sete Lagoas-MG, Brasil
Registered: 2013-01-20
Posts: 250

Re: Restfull Validation

Hi there, I've tested the TSynFilterUppercase and worked as expected, but it seems that auto validate is not working as expected when you uses MinLength and  MaxLength parameters.

An workaround would be at client side, to handle manually, something like this:

var aFilter : TSynValidateText;
msg : string;
----------
msg := '';
aFilter := TSynValidateText.Create('{"MinLength":5,"MaxLength":10}');
TSQLPersona.AddFilterOrValidate('Name',aFilter);
{
aPersona.Filter();
aPersona.Validate(aClient);
}
....
aPersona.Name := 'Nam'+Int32ToUtf8(Random(10000));
...
if (aFilter.Process(0,aPersona.Name,msg)) then begin
aClient.Add(Rec,true);

Last edited by warleyalex (2015-04-02 15:44:56)

Offline

#11 2015-04-02 16:34:14

ws
Member
Registered: 2015-03-17
Posts: 19

Re: Restfull Validation

ok, now I understand. I'll need to use Delphi to my client app, and if I am working with client made with html / css / js, is it possible that my server app return me an error message if there was any violation in my restrictions?

it would be possible when making an insert / update / delete and there was no error return a message for example: {"success": "ok"} on error: {"error": "Message"}

Offline

Board footer

Powered by FluxBB