Comments (8)
This is finally available in v0.8.4 😃
https://crates.io/crates/schemars/0.8.4
from schemars.
There's nothing like this implemented yet, but it is something I've thought about.
One way of specifying validations that #[derive(JsonSchema)]
could hook into would be using something like the validator crate's validate
attributes.
Any fields with a validate
attribute could have the appropriate JSON Schema validation properties and/or format
property set. e.g.
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 #[schemars(...)]
attributes to allow setting more properties on the generated schema
from schemars.
@GREsau I like the approach with the validate
attribute!
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).
from schemars.
Sure, I'd be happy to accept a PR for this
from schemars.
@GREsau @patriksvensson I've made some effort towards validation in #40. If that approach is accepted, I'd advise just setting fields with schemars(max_items = 1, ...)
so that integration is still possible with validator.
from schemars.
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.
from schemars.
Would it make sense to use Valico's DSL to generate the schema?
- It would be inherently consistent with the goal of this project.
- It might be possible to reduce the glue and pass attributes' options generically.
- And going with custom
#[schemars(...)]
attributes might be the safest choice in case thevalidator
crate, or any other generic validator, were to have conflicts with the JSONSchema standard?
from schemars.
Hello,
I'm interested into this feature too.
I'm just wondering if it's not a SerDe feature ?
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:
- Deserializer with internal check while parsing a file
- implement a setter to update the value, validating first the regex
Like that Schemars can use that parameter to ad the right field in the generated schema.
Does it make sense ?
from schemars.
Related Issues (20)
- Map schemas do not specify the key type
- FEEDBACK WANTED - API proposal - supporting multiple JSON schema versions, breaking changes to `Schema` definition HOT 2
- Impl JsonSchema for for rocket_validation::Validated
- Generating definition types for enums with serde tag HOT 3
- Huge LLVM line count, quadratic compile time for derive(JsonSchema) HOT 7
- default member missing in schema for generic type that implements Default HOT 2
- Support for no_std
- Just can't enable smol_str feature
- Kubernetes extensions
- Schemars omit serde(flatten) BTreeMap HOT 3
- Override Serde for schema generation only
- Switch to indexmap2 for aarch64
- Consider updating `schemars_derive` to use `syn` v2 HOT 1
- Allow #[schemars(with = "Type")] on struct HOT 1
- Support Avro schema generation?
- Example for JsonSchema without definition references HOT 1
- Maintenance status HOT 1
- enums: serde rename_all_fields not respected
- Schemars can't deal with TOML files HOT 2
- Annotate a field so it is both `required` and `default`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from schemars.