icydb_model/base/validator/collection.rs
1//! Module: base::validator::collection
2//!
3//! Responsibility: base validator definitions.
4//! Does not own: normalization policy, persistence, or schema mutation semantics.
5//! Boundary: reports typed visitor issues for facade schema values.
6
7use crate::{
8 prelude::*,
9 visitor::{Validator, VisitorContext},
10};
11
12///
13/// InArray
14///
15/// Validates that an input value appears in a fixed allow-list.
16/// This is useful for small enum-like domains represented as raw values.
17///
18
19#[validator]
20pub struct InArray<T> {
21 values: Vec<T>,
22}
23
24impl<T> InArray<T> {
25 /// Builds an allow-list validator from the provided set of accepted values.
26 #[must_use]
27 pub const fn new(values: Vec<T>) -> Self {
28 Self { values }
29 }
30}
31
32impl<T> Validator<T> for InArray<T>
33where
34 T: PartialEq,
35{
36 fn validate(&self, n: &T, ctx: &mut dyn VisitorContext) {
37 if !self.values.contains(n) {
38 ctx.issue(format!(
39 "value must be one of {} allowed values",
40 self.values.len()
41 ));
42 }
43 }
44}