Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ build
docs
.DS_Store
dist
target
87 changes: 86 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,5 @@ license = "Apache-2.0"

[dependencies]
serde_json = "1.0"

[dependencies.serde]
version = "1.0"
features = [ "derive" ]
serde = { version = "1.0", features = [ "derive" ] }
derive_builder = "0.10.2"
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,40 @@ This repo contains the json schema meta schema and code to package it on npm, ge

`go get github.com/json-schema-tools/meta-schema`


### Rust

`cargo install json_schema`

## Using

### Typescript
```typescript
import JSONSchema, { JSONSchemaObject, Properties, Items } from "@json-schema-tools/meta-schema"
```

###
### Rust

#### From a string
```rust
let foo = r#"{
"title": "helloworld",
"type": "string"
}"#;

let as_json_schema: JSONSchemaObject = serde_json::from_str(foo).unwrap();
```

#### Using builder pattern
```rust
let schema = JSONSchemaObjectBuilder::default()
.title("foobar".to_string())
._type(Type::SimpleTypes(SimpleTypes::String))
.build()
.unwrap();

let as_str = serde_json::to_string(&schema).unwrap();
```

### Contributing

Expand Down
108 changes: 75 additions & 33 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
extern crate serde;
extern crate serde_json;
extern crate derive_builder;

use derive_builder::Builder;
use serde::{Serialize, Deserialize};
use std::collections::HashMap;

pub type Id = String;
pub type Schema = String;
pub type Ref = String;
Expand All @@ -21,7 +24,8 @@ pub type NonNegativeInteger = i64;
pub type NonNegativeIntegerDefaultZero = i64;
pub type Pattern = String;
pub type SchemaArray = Vec<JSONSchema>;
#[derive(Serialize, Deserialize)]

#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq)]
pub enum Items {
JSONSchema,
SchemaArray
Expand All @@ -48,44 +52,80 @@ pub type Definitions = HashMap<String, Option<serde_json::Value>>;
///
/// {}
///
pub type Properties = HashMap<String, Option<serde_json::Value>>;

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Builder, Default)]
#[builder(setter(strip_option), default)]
#[serde(default)]
pub struct Properties {
#[serde(flatten)]
pub additional_properties: Option<PropertiesAdditional>
}
pub type PropertiesAdditional = HashMap<String, Box<JSONSchema>>;
/// PatternProperties
///
/// # Default
///
/// {}
///
pub type PatternProperties = HashMap<String, Option<serde_json::Value>>;
#[derive(Serialize, Deserialize)]

#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq)]
pub enum DependenciesSet {
JSONSchema,
StringArray
}
pub type Dependencies = HashMap<String, Option<serde_json::Value>>;
pub type Enum = Vec<AlwaysTrue>;
pub type SimpleTypes = serde_json::Value;

#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq)]
pub enum SimpleTypes {
#[serde(rename = "string")]
String,
#[serde(rename = "array")]
Array,
#[serde(rename = "object")]
Object,
#[serde(rename = "number")]
Number,
#[serde(rename = "boolean")]
Boolean,
#[serde(rename = "integer")]
Integer
}

pub type ArrayOfSimpleTypes = Vec<SimpleTypes>;
#[derive(Serialize, Deserialize)]

#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq)]
#[serde(untagged)]
pub enum Type {
SimpleTypes,
ArrayOfSimpleTypes
SimpleTypes(SimpleTypes),
ArrayOfSimpleTypes(ArrayOfSimpleTypes)
}

pub type Format = String;
pub type ContentMediaType = String;
pub type ContentEncoding = String;
#[derive(Serialize, Deserialize)]

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Builder, Default)]
#[builder(setter(strip_option), default)]
#[serde(default)]
pub struct JSONSchemaObject {
#[serde(rename="$id")]
pub(crate) id: Option<Id>,
#[serde(rename="$schema")]
pub(crate) schema: Option<Schema>,
#[serde(rename="$ref")]
pub(crate) _ref: Option<Ref>,
#[serde(rename="$comment")]
pub(crate) comment: Option<Comment>,
pub(crate) title: Option<Title>,
#[serde(rename="$id", skip_serializing_if = "Option::is_none")]
pub id: Option<Id>,
#[serde(rename="$schema", skip_serializing_if = "Option::is_none")]
pub schema: Option<Schema>,
#[serde(rename="$ref", skip_serializing_if = "Option::is_none")]
pub _ref: Option<Ref>,
#[serde(rename="$comment", skip_serializing_if = "Option::is_none")]
pub comment: Option<Comment>,
#[serde(skip_serializing_if = "Option::is_none")]
pub title: Option<Title>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(crate) description: Option<Description>,
pub(crate) default: Option<AlwaysTrue>,

#[serde(rename="default", skip_serializing_if = "Option::is_none")]
pub(crate) _default: Option<AlwaysTrue>,

#[serde(rename="readOnly")]
pub(crate) read_only: Option<ReadOnly>,
pub(crate) examples: Option<Examples>,
Expand All @@ -103,60 +143,62 @@ pub struct JSONSchemaObject {
pub(crate) min_length: Option<NonNegativeIntegerDefaultZero>,
pub(crate) pattern: Option<Pattern>,
#[serde(rename="additionalItems")]
pub(crate) additional_items: Option<JSONSchema>,
pub(crate) additional_items: Option<Box<JSONSchema>>,
pub(crate) items: Option<Items>,
#[serde(rename="maxItems")]
pub(crate) max_items: Option<NonNegativeInteger>,
#[serde(rename="minItems")]
pub(crate) min_items: Option<NonNegativeIntegerDefaultZero>,
#[serde(rename="uniqueItems")]
pub(crate) unique_items: Option<UniqueItems>,
pub(crate) contains: Option<JSONSchema>,
pub contains: Option<Box<JSONSchema>>,
#[serde(rename="maxProperties")]
pub(crate) max_properties: Option<NonNegativeInteger>,
#[serde(rename="minProperties")]
pub(crate) min_properties: Option<NonNegativeIntegerDefaultZero>,
pub(crate) required: Option<StringArray>,
#[serde(rename="additionalProperties")]
pub(crate) additional_properties: Option<JSONSchema>,
pub(crate) additional_properties: Option<Box<JSONSchema>>,
pub(crate) definitions: Option<Definitions>,
pub(crate) properties: Option<Properties>,
pub properties: Option<Properties>,
#[serde(rename="patternProperties")]
pub(crate) pattern_properties: Option<PatternProperties>,
pub(crate) dependencies: Option<Dependencies>,
#[serde(rename="propertyNames")]
pub(crate) property_names: Option<JSONSchema>,
pub(crate) property_names: Option<Box<JSONSchema>>,
#[serde(rename="const")]
pub(crate) _const: Option<AlwaysTrue>,
#[serde(rename="enum")]
pub(crate) _enum: Option<Enum>,
#[serde(rename="type")]
pub(crate) _type: Option<Type>,
pub _type: Option<Type>,
pub(crate) format: Option<Format>,
#[serde(rename="contentMediaType")]
pub(crate) content_media_type: Option<ContentMediaType>,
#[serde(rename="contentEncoding")]
pub(crate) content_encoding: Option<ContentEncoding>,
#[serde(rename="if")]
pub(crate) _if: Option<JSONSchema>,
pub(crate) then: Option<JSONSchema>,
pub(crate) _if: Option<Box<JSONSchema>>,
pub(crate) then: Option<Box<JSONSchema>>,
#[serde(rename="else")]
pub(crate) _else: Option<JSONSchema>,
pub(crate) _else: Option<Box<JSONSchema>>,
#[serde(rename="allOf")]
pub(crate) all_of: Option<SchemaArray>,
#[serde(rename="anyOf")]
pub(crate) any_of: Option<SchemaArray>,
pub(crate) any_of: Option<Box<JSONSchema>>,
#[serde(rename="oneOf")]
pub(crate) one_of: Option<SchemaArray>,
pub(crate) not: Option<JSONSchema>,
pub(crate) not: Option<Box<JSONSchema>>,
}
/// JSONSchemaBoolean
///
/// Always valid if true. Never valid if false. Is constant.
///
pub type JSONSchemaBoolean = bool;
#[derive(Serialize, Deserialize)]

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(untagged)]
pub enum JSONSchema {
JSONSchemaObject,
JSONSchemaBoolean
}
JSONSchemaObject(JSONSchemaObject),
JSONSchemaBoolean(JSONSchemaBoolean)
}
Loading