-
-
Notifications
You must be signed in to change notification settings - Fork 231
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
Validation support #12
Comments
There's nothing like this implemented yet, but it is something I've thought about. One way of specifying validations that Any fields with a lazy_static! {
static ref USERNAME_RE: Regex = Regex::new(r"^\w{2,}$").unwrap();
}
#[derive(JsonSchema)]
pub struct User{
#[validate(regex = "USERNAME_RE")]
pub username: String,
#[validate(email)]
pub email: String,
} Could produce a schema like: {
"title": "User",
"type": "object",
"properties": {
"username": {
"type": "string",
"pattern": "^\\w{2,}$"
},
"email": {
"type": "string",
"format": "email"
}
}
} Alternatively, I could just add more options to |
@GREsau I like the approach with the I would be happy to try to submit a pull request for this unless this is something you want to do yourself (if it's something that you would consider adding to Schemars that is). |
Sure, I'd be happy to accept a PR for this |
@GREsau @patriksvensson I've made some effort towards validation in #40. If that approach is accepted, I'd advise just setting fields with |
I've created a validation library based on #40 that provides integration with Schemars. Types can validate themselves by generating a schema and validating against it. The generated schema just has to contain all validation-related fields. I could start working on a PR to set those fields with attributes. What would be the best way to get info about fields in the derive macro? I would personally like to deny attributes that make no sense for a type. |
Would it make sense to use Valico's DSL to generate the schema?
|
Hello, I'm interested into this feature too. For me I expect more to define something like: #[derive(Serialize, Deserialize, JsonSchema)]
pub struct User{
#[serde(regex = "^\w{2,}$", getter = true, setter = true)]
username: String,
} So like that Serde can generate:
Like that Schemars can use that parameter to ad the right field in the generated schema. |
This is finally available in v0.8.4 😃 |
Hello again, sorry for cluttering your inbox with GitHub issues.
I've just taken a quick look so far, but I see that the source code for Schemars support validations like
pattern
for strings (https://github.com/GREsau/schemars/blob/master/schemars/src/schema.rs#L364). Is there any way of adding validations to a schema? I couldn't find any information on the site about this, so that's why I'm asking.The text was updated successfully, but these errors were encountered: