Skip to content

Add id validation for entity parsing - #86

Merged
DZakh merged 2 commits into
mainfrom
dz/entity-id-validation
Jul 26, 2024
Merged

Add id validation for entity parsing#86
DZakh merged 2 commits into
mainfrom
dz/entity-id-validation

Conversation

@DZakh

@DZakh DZakh commented Jul 24, 2024

Copy link
Copy Markdown
Member

No description provided.

@DZakh
DZakh requested a review from JonoPrest July 24, 2024 15:51
Comment on lines +370 to +377
let has_id = obj.fields.iter().any(|field| field.name == "id");
if !has_id {
return Err(anyhow!(
"No 'id' field found on entity {}. Please add an 'id' field to your entity.",
name
));
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My preference would be to have this in a separate validation function rather than during parsing. But it accomplishes the same goal so will leave it up to you.

It should just be clear when we need another validator where it should go so that we don't end up with distributed validation.

Perhaps for example we implement a method on Entity called "validate_has_id_field" or something. That takes an entity and returns a result of entity so we can chain validators like you can see in some other places.

Then have a root constructor function that runs all needed validators.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My preference would be to have this in a separate validation function rather than during parsing. But it accomplishes the same goal so will leave it up to you.

I personally prefer keeping it in the parser, than having a separate validation function. https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/

But, what could be good is to move the logic to validate_has_id_field as you said, and use it in the entity creation function to improve readability.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally prefer keeping it in the parser, than having a separate validation function.

I get the point but what I'm suggesting is essentially still in the parse step if validation occurs in a constructor function. That way you still keep the check before the type can be created. It's more similar separating lexing from parsing. I'm essentially just saying we should add it in a way that is obvious what the pattern is for the next person that comes along to add a constraint.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Do you mean to put it where the Self is actually created?

@JonoPrest JonoPrest left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leave it up to you but take a look at my comment 👍🏼

@DZakh
DZakh merged commit f3b8740 into main Jul 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants