#1 2026-01-28 17:37:02

testgary
Member
Registered: 2025-02-06
Posts: 49

How to Elegantly Validate Input Length

TUserDTO = packed record      
  ID: TID;                    
  LogonName: RawUtf8;         
  DisplayName: RawUtf8;       
  InviteCode: RawUtf8;        
  ReferralCode: RawUtf8;      
  GroupName: RawUtf8;         
  Email: RawUtf8;             
  Phone: RawUtf8;             
  AvatarURL: RawUtf8;         
  Gender: TGenderTypeEnum;    
  Birthdate: TTimeLog;        
  Country: RawUtf8;           
  Province: RawUtf8;          
  City: RawUtf8;              
...       
end;                          

lazarus + mormot 2 + mongodb

How to Elegantly Validate Input Length?

Offline

#2 2026-01-29 10:31:47

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 15,401
Website

Re: How to Elegantly Validate Input Length

The TSynFilter and TSynValidation classes are currently for ORM only...

You could be tempted to define a reusable function() returning an enumeration or a string to make it on the client/DTO side.
But since it may be very depending of each use case of this DTO, it may not be easy to reuse this function.

Perhaps, the cleanest is to put the logic on the server side, having the application layer check the DTO sent by the client, depending on its actual context call.
With an application layer, the API method/URI is tied to the application context, so it knows enough about the context to validate the input.
And only having basic validation, e.g. at the UI level, for most obvious issue.
This would ensure that the very same logic could be reuse if you use another kind of client (e.g. web app).

Of course, there will be final context validation at persistence level, because your persistence service would not trust the application layer, and could reject length.
Note that input length is likely to be depending on the backend database. It is better in 2026 to have no size limit on the database field length.

Offline

#3 2026-01-29 13:31:15

testgary
Member
Registered: 2025-02-06
Posts: 49

Re: How to Elegantly Validate Input Length

class procedure TOrmUser.InternalDefineModel(Props: TOrmProperties);
begin
  inherited InternalDefineModel(Props);
  AddFilterOrValidate('Phone', TSynValidatePhone.Create);
  AddFilterOrValidate('Email', TSynValidateEmail.Create);
  ...
end; 

Is this how it's written?

Offline

#4 2026-01-30 14:56:13

mrbar2000
Member
From: Brazil
Registered: 2016-10-26
Posts: 122

Re: How to Elegantly Validate Input Length

I have studied Python, FastAPI, pyDantic

It is a combination very simple, fast and I think that many things could by bring to mormot.

pyDantic valid DTO on server when receive request and generate exception if some field is invalid automaticaly.
U just have register the trules on DTO

from fastapi import FastAPI
from typing import List
from pydantic import BaseModel, Field

-----------------
app = FastAPI()

class Student(BaseModel):
   id: int
   name :str = Field(None, title="name of student", max_length=10)
   subjects: List[str] = []

@app.post("/students/")
async def student_data(s1: Student):
   return s1
----------------
just this to have a Api online....

U can use RegularExpression, required, etc on field´s method
FastAPI gegnerate all documentation too. no needs external tools.

Just ideas. not is a mormot critc! I love Mormot, I love u Arnould! smile

Offline

#5 2026-02-01 04:07:06

testgary
Member
Registered: 2025-02-06
Posts: 49

Re: How to Elegantly Validate Input Length

Thank you very much for sharing.

Offline

Board footer

Powered by FluxBB