Fix indentation
It should be 4 spaces, not 2
This commit is contained in:
parent
e151d21d6d
commit
f55572f735
2 changed files with 211 additions and 211 deletions
160
README.md
160
README.md
|
@ -38,55 +38,55 @@ fn main() {
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||||
"title": "MyStruct",
|
"title": "MyStruct",
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"required": [
|
"required": [
|
||||||
"my_bool",
|
"my_bool",
|
||||||
"my_int",
|
"my_int",
|
||||||
"my_nullable_enum"
|
"my_nullable_enum"
|
||||||
],
|
],
|
||||||
"properties": {
|
"properties": {
|
||||||
"my_bool": {
|
"my_bool": {
|
||||||
"type": "boolean"
|
"type": "boolean"
|
||||||
},
|
|
||||||
"my_int": {
|
|
||||||
"type": "integer",
|
|
||||||
"format": "int32"
|
|
||||||
},
|
|
||||||
"my_nullable_enum": {
|
|
||||||
"anyOf": [
|
|
||||||
{
|
|
||||||
"$ref": "#/definitions/MyEnum"
|
|
||||||
},
|
},
|
||||||
{
|
"my_int": {
|
||||||
"type": "null"
|
"type": "integer",
|
||||||
}
|
"format": "int32"
|
||||||
]
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"definitions": {
|
|
||||||
"MyEnum": {
|
|
||||||
"anyOf": [
|
|
||||||
{
|
|
||||||
"enum": [
|
|
||||||
"Unit"
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
{
|
"my_nullable_enum": {
|
||||||
"type": "object",
|
"anyOf": [
|
||||||
"required": [
|
{
|
||||||
"StringNewType"
|
"$ref": "#/definitions/MyEnum"
|
||||||
],
|
},
|
||||||
"properties": {
|
{
|
||||||
"StringNewType": {
|
"type": "null"
|
||||||
"type": "string"
|
}
|
||||||
}
|
]
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"definitions": {
|
||||||
|
"MyEnum": {
|
||||||
|
"anyOf": [
|
||||||
|
{
|
||||||
|
"enum": [
|
||||||
|
"Unit"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "object",
|
||||||
|
"required": [
|
||||||
|
"StringNewType"
|
||||||
|
],
|
||||||
|
"properties": {
|
||||||
|
"StringNewType": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
]
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
</details>
|
</details>
|
||||||
|
@ -128,45 +128,45 @@ fn main() {
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||||
"title": "MyStruct",
|
"title": "MyStruct",
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"required": [
|
"required": [
|
||||||
"myBool",
|
"myBool",
|
||||||
"myNumber"
|
"myNumber"
|
||||||
],
|
],
|
||||||
"properties": {
|
"properties": {
|
||||||
"myBool": {
|
"myBool": {
|
||||||
"type": "boolean"
|
"type": "boolean"
|
||||||
},
|
|
||||||
"myNullableEnum": {
|
|
||||||
"default": null,
|
|
||||||
"anyOf": [
|
|
||||||
{
|
|
||||||
"$ref": "#/definitions/MyEnum"
|
|
||||||
},
|
},
|
||||||
{
|
"myNullableEnum": {
|
||||||
"type": "null"
|
"default": null,
|
||||||
}
|
"anyOf": [
|
||||||
]
|
{
|
||||||
},
|
"$ref": "#/definitions/MyEnum"
|
||||||
"myNumber": {
|
},
|
||||||
"type": "integer",
|
{
|
||||||
"format": "int32"
|
"type": "null"
|
||||||
}
|
}
|
||||||
},
|
]
|
||||||
"definitions": {
|
|
||||||
"MyEnum": {
|
|
||||||
"anyOf": [
|
|
||||||
{
|
|
||||||
"type": "null"
|
|
||||||
},
|
},
|
||||||
{
|
"myNumber": {
|
||||||
"type": "string"
|
"type": "integer",
|
||||||
|
"format": "int32"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"definitions": {
|
||||||
|
"MyEnum": {
|
||||||
|
"anyOf": [
|
||||||
|
{
|
||||||
|
"type": "null"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
]
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
</details>
|
</details>
|
||||||
|
|
|
@ -32,55 +32,55 @@ fn main() {
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||||
"title": "MyStruct",
|
"title": "MyStruct",
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"required": [
|
"required": [
|
||||||
"my_bool",
|
"my_bool",
|
||||||
"my_int",
|
"my_int",
|
||||||
"my_nullable_enum"
|
"my_nullable_enum"
|
||||||
],
|
],
|
||||||
"properties": {
|
"properties": {
|
||||||
"my_bool": {
|
"my_bool": {
|
||||||
"type": "boolean"
|
"type": "boolean"
|
||||||
},
|
|
||||||
"my_int": {
|
|
||||||
"type": "integer",
|
|
||||||
"format": "int32"
|
|
||||||
},
|
|
||||||
"my_nullable_enum": {
|
|
||||||
"anyOf": [
|
|
||||||
{
|
|
||||||
"$ref": "#/definitions/MyEnum"
|
|
||||||
},
|
},
|
||||||
{
|
"my_int": {
|
||||||
"type": "null"
|
"type": "integer",
|
||||||
}
|
"format": "int32"
|
||||||
]
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"definitions": {
|
|
||||||
"MyEnum": {
|
|
||||||
"anyOf": [
|
|
||||||
{
|
|
||||||
"enum": [
|
|
||||||
"Unit"
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
{
|
"my_nullable_enum": {
|
||||||
"type": "object",
|
"anyOf": [
|
||||||
"required": [
|
{
|
||||||
"StringNewType"
|
"$ref": "#/definitions/MyEnum"
|
||||||
],
|
},
|
||||||
"properties": {
|
{
|
||||||
"StringNewType": {
|
"type": "null"
|
||||||
"type": "string"
|
}
|
||||||
}
|
]
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"definitions": {
|
||||||
|
"MyEnum": {
|
||||||
|
"anyOf": [
|
||||||
|
{
|
||||||
|
"enum": [
|
||||||
|
"Unit"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "object",
|
||||||
|
"required": [
|
||||||
|
"StringNewType"
|
||||||
|
],
|
||||||
|
"properties": {
|
||||||
|
"StringNewType": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
]
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
</details>
|
</details>
|
||||||
|
@ -122,49 +122,49 @@ fn main() {
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||||
"title": "MyStruct",
|
"title": "MyStruct",
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"required": [
|
"required": [
|
||||||
"myBool",
|
"myBool",
|
||||||
"myNumber"
|
"myNumber"
|
||||||
],
|
],
|
||||||
"properties": {
|
"properties": {
|
||||||
"myBool": {
|
"myBool": {
|
||||||
"type": "boolean"
|
"type": "boolean"
|
||||||
},
|
|
||||||
"myNullableEnum": {
|
|
||||||
"anyOf": [
|
|
||||||
{
|
|
||||||
"$ref": "#/definitions/MyEnum"
|
|
||||||
},
|
},
|
||||||
{
|
"myNullableEnum": {
|
||||||
"type": "null"
|
"default": null,
|
||||||
}
|
"anyOf": [
|
||||||
]
|
{
|
||||||
},
|
"$ref": "#/definitions/MyEnum"
|
||||||
"myNumber": {
|
},
|
||||||
"type": "integer",
|
{
|
||||||
"format": "int32"
|
"type": "null"
|
||||||
}
|
}
|
||||||
},
|
]
|
||||||
"definitions": {
|
|
||||||
"MyEnum": {
|
|
||||||
"anyOf": [
|
|
||||||
{
|
|
||||||
"type": "null"
|
|
||||||
},
|
},
|
||||||
{
|
"myNumber": {
|
||||||
"type": "string"
|
"type": "integer",
|
||||||
|
"format": "int32"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"definitions": {
|
||||||
|
"MyEnum": {
|
||||||
|
"anyOf": [
|
||||||
|
{
|
||||||
|
"type": "null"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
]
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
\
|
|
||||||
`#[serde(...)]` attributes can be overriden using `#[schemars(...)]` attributes, which behave identically (e.g. `#[schemars(rename_all = "camelCase")]`). You may find this useful if you want to change the generated schema without affecting Serde's behaviour, or if you're just not using Serde.
|
`#[serde(...)]` attributes can be overriden using `#[schemars(...)]` attributes, which behave identically (e.g. `#[schemars(rename_all = "camelCase")]`). You may find this useful if you want to change the generated schema without affecting Serde's behaviour, or if you're just not using Serde.
|
||||||
|
|
||||||
## Feature Flags
|
## Feature Flags
|
||||||
|
@ -219,64 +219,64 @@ use schema::Schema;
|
||||||
/// let my_schema = schema_for!(MyStruct);
|
/// let my_schema = schema_for!(MyStruct);
|
||||||
/// ```
|
/// ```
|
||||||
pub trait JsonSchema {
|
pub trait JsonSchema {
|
||||||
/// Whether JSON Schemas generated for this type should be re-used where possible using the `$ref` keyword.
|
/// Whether JSON Schemas generated for this type should be re-used where possible using the `$ref` keyword.
|
||||||
///
|
///
|
||||||
/// For trivial types (such as primitives), this should return `false`. For more complex types, it should return `true`.
|
/// For trivial types (such as primitives), this should return `false`. For more complex types, it should return `true`.
|
||||||
/// For recursive types, this *must* return `true` to prevent infinite cycles when generating schemas.
|
/// For recursive types, this *must* return `true` to prevent infinite cycles when generating schemas.
|
||||||
///
|
///
|
||||||
/// By default, this returns `true`.
|
/// By default, this returns `true`.
|
||||||
fn is_referenceable() -> bool {
|
fn is_referenceable() -> bool {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The name of the generated JSON Schema.
|
/// The name of the generated JSON Schema.
|
||||||
///
|
///
|
||||||
/// This is used as the title for root schemas, and the key within the `definitions` property for subschemas.
|
/// This is used as the title for root schemas, and the key within the `definitions` property for subschemas.
|
||||||
fn schema_name() -> String;
|
fn schema_name() -> String;
|
||||||
|
|
||||||
/// Generates a JSON Schema for this type.
|
/// Generates a JSON Schema for this type.
|
||||||
///
|
///
|
||||||
/// If the returned schema depends on any [referenceable](JsonSchema::is_referenceable) schemas, then this method will
|
/// If the returned schema depends on any [referenceable](JsonSchema::is_referenceable) schemas, then this method will
|
||||||
/// add them to the [`SchemaGenerator`](gen::SchemaGenerator)'s schema definitions.
|
/// add them to the [`SchemaGenerator`](gen::SchemaGenerator)'s schema definitions.
|
||||||
///
|
///
|
||||||
/// This should not return a `$ref` schema.
|
/// This should not return a `$ref` schema.
|
||||||
fn json_schema(gen: &mut gen::SchemaGenerator) -> Schema;
|
fn json_schema(gen: &mut gen::SchemaGenerator) -> Schema;
|
||||||
|
|
||||||
/// Helper for generating schemas for flattened `Option` fields.
|
/// Helper for generating schemas for flattened `Option` fields.
|
||||||
///
|
///
|
||||||
/// This should not need to be called or implemented by code outside of `schemars`.
|
/// This should not need to be called or implemented by code outside of `schemars`.
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
fn json_schema_optional(gen: &mut gen::SchemaGenerator) -> Schema {
|
fn json_schema_optional(gen: &mut gen::SchemaGenerator) -> Schema {
|
||||||
Self::json_schema(gen)
|
Self::json_schema(gen)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
pub mod tests {
|
pub mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
pub fn schema_object_for<T: JsonSchema>() -> schema::SchemaObject {
|
pub fn schema_object_for<T: JsonSchema>() -> schema::SchemaObject {
|
||||||
schema_object(schema_for::<T>())
|
schema_object(schema_for::<T>())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn custom_schema_object_for<T: JsonSchema>(
|
pub fn custom_schema_object_for<T: JsonSchema>(
|
||||||
settings: gen::SchemaSettings,
|
settings: gen::SchemaSettings,
|
||||||
) -> schema::SchemaObject {
|
) -> schema::SchemaObject {
|
||||||
schema_object(custom_schema_for::<T>(settings))
|
schema_object(custom_schema_for::<T>(settings))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn schema_for<T: JsonSchema>() -> schema::Schema {
|
pub fn schema_for<T: JsonSchema>() -> schema::Schema {
|
||||||
custom_schema_for::<T>(Default::default())
|
custom_schema_for::<T>(Default::default())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn custom_schema_for<T: JsonSchema>(settings: gen::SchemaSettings) -> schema::Schema {
|
pub fn custom_schema_for<T: JsonSchema>(settings: gen::SchemaSettings) -> schema::Schema {
|
||||||
T::json_schema(&mut gen::SchemaGenerator::new(settings))
|
T::json_schema(&mut gen::SchemaGenerator::new(settings))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn schema_object(schema: schema::Schema) -> schema::SchemaObject {
|
pub fn schema_object(schema: schema::Schema) -> schema::SchemaObject {
|
||||||
match schema {
|
match schema {
|
||||||
schema::Schema::Object(o) => o,
|
schema::Schema::Object(o) => o,
|
||||||
s => panic!("Schema was not an object: {:?}", s),
|
s => panic!("Schema was not an object: {:?}", s),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue