You are not logged in.
Pages: 1
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
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
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
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! ![]()
Offline
Thank you very much for sharing.
Offline
Pages: 1