Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow for a row to be validated with custom logic prior to appending it (in any way) #8

Closed
GlenDC opened this issue Apr 18, 2024 · 0 comments · Fixed by #9
Closed
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@GlenDC
Copy link
Member

GlenDC commented Apr 18, 2024

Rust's type system helps a lot with validation, given plenty of assumptions and requirements can be registered within the used types. At times however the combination of properties give meaning as a group, where some combinations make sense and others not. Enums (sum types) are what help usually in this situation. However it is not always a desired one, and in the case of venndb it doesn't play very well with the rest of the setup. As such for that reason and others it can be useful to allow for a custom validation of a row prior to appending it, such that one can guarantee that any appended rows do fulfill all required requirements.

Example:

#[derive(Debug, VennDB)]
#[venndb(name = "MyDB", validatator = "my_validator_fn")]
pub struct Value {
   pub foo: String,
   pub bar: u32,
}

fn my_validator_fn(value: &Value) -> bool {
    !foo.is_empty() && value.bar > 0
}

let mut db = MyDB::default();
assert!(db.append(Value {
    foo: "".to_owned(),
    bar: 42,
}).is_err()); // fails because foo == empty

The above example illustrates that the type system can only bring you so far.
Of course one can argue that a NewType can solve this particular issue by only allowing the newtype
instances to be created with values that match such conditions. And this is true.

However, what if the legal value space of a column depends upon the specific value of another?
E.g. what is foo can be empty when bar is 2. This becomes a lot more difficult, and given the
large space that unsigned integers are composed from it might also become impossible
to abstract such rules in a sum type. If desired at all of course, given it would also make
the usage of it within a venndb-derived db more difficult.

@GlenDC GlenDC added the enhancement New feature or request label Apr 18, 2024
@GlenDC GlenDC added this to the 0.4.0 milestone Apr 18, 2024
@GlenDC GlenDC self-assigned this Apr 18, 2024
@GlenDC GlenDC mentioned this issue Apr 18, 2024
@GlenDC GlenDC closed this as completed in #9 Apr 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant