rust_json_schema 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.standard.yml +3 -0
- data/Cargo.lock +1551 -0
- data/Cargo.toml +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +35 -0
- data/Rakefile +18 -0
- data/ext/rust_json_schema/Cargo.toml +15 -0
- data/ext/rust_json_schema/extconf.rb +6 -0
- data/ext/rust_json_schema/src/lib.rs +101 -0
- data/lib/rust_json_schema/version.rb +5 -0
- data/lib/rust_json_schema.rb +11 -0
- data/sig/rust_json_schema.rbs +4 -0
- metadata +60 -0
data/Cargo.toml
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 Taylor Thurlow
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# RustJSONSchema
|
2
|
+
|
3
|
+
TODO: Delete this and the text below, and describe your gem
|
4
|
+
|
5
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/rust_json_schema`. To experiment with that code, run `bin/console` for an interactive prompt.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
|
10
|
+
|
11
|
+
Install the gem and add to the application's Gemfile by executing:
|
12
|
+
|
13
|
+
$ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
|
14
|
+
|
15
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
16
|
+
|
17
|
+
$ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Development
|
24
|
+
|
25
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
26
|
+
|
27
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
28
|
+
|
29
|
+
## Contributing
|
30
|
+
|
31
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rust_json_schema.
|
32
|
+
|
33
|
+
## License
|
34
|
+
|
35
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "rspec/core/rake_task"
|
5
|
+
|
6
|
+
RSpec::Core::RakeTask.new(:spec)
|
7
|
+
|
8
|
+
require "standard/rake"
|
9
|
+
|
10
|
+
require "rb_sys/extensiontask"
|
11
|
+
|
12
|
+
task build: :compile
|
13
|
+
|
14
|
+
RbSys::ExtensionTask.new("rust_json_schema") do |ext|
|
15
|
+
ext.lib_dir = "lib/rust_json_schema"
|
16
|
+
end
|
17
|
+
|
18
|
+
task default: %i[compile spec standard]
|
@@ -0,0 +1,15 @@
|
|
1
|
+
[package]
|
2
|
+
name = "rust_json_schema"
|
3
|
+
version = "0.1.0"
|
4
|
+
edition = "2021"
|
5
|
+
authors = ["Taylor Thurlow <thurlow@hey.com>"]
|
6
|
+
license = "MIT"
|
7
|
+
publish = false
|
8
|
+
|
9
|
+
[lib]
|
10
|
+
crate-type = ["cdylib"]
|
11
|
+
|
12
|
+
[dependencies]
|
13
|
+
jsonschema = "0.17.1"
|
14
|
+
magnus = "0.6"
|
15
|
+
serde_json = "1.0"
|
@@ -0,0 +1,101 @@
|
|
1
|
+
use magnus::{
|
2
|
+
exception::ExceptionClass, function, gc::register_mark_object, method, prelude::*, value::Lazy,
|
3
|
+
wrap, RModule, Ruby,
|
4
|
+
};
|
5
|
+
|
6
|
+
extern crate serde_json;
|
7
|
+
use jsonschema::JSONSchema;
|
8
|
+
|
9
|
+
#[wrap(class = "RustJSONSchema::Validator")]
|
10
|
+
struct Validator {
|
11
|
+
schema: JSONSchema,
|
12
|
+
}
|
13
|
+
|
14
|
+
impl Validator {
|
15
|
+
fn new(ruby: &Ruby, json: String) -> Result<Validator, magnus::Error> {
|
16
|
+
let value: serde_json::Value = match serde_json::from_str(&json) {
|
17
|
+
Ok(value) => value,
|
18
|
+
Err(error) => {
|
19
|
+
return Err(magnus::Error::new(
|
20
|
+
ruby.get_inner(&JSON_PARSE_ERROR),
|
21
|
+
error.to_string(),
|
22
|
+
))
|
23
|
+
}
|
24
|
+
};
|
25
|
+
|
26
|
+
let schema = match JSONSchema::options().compile(&value) {
|
27
|
+
Ok(schema) => schema,
|
28
|
+
Err(error) => {
|
29
|
+
return Err(magnus::Error::new(
|
30
|
+
ruby.get_inner(&SCHEMA_PARSE_ERROR),
|
31
|
+
error.to_string(),
|
32
|
+
))
|
33
|
+
}
|
34
|
+
};
|
35
|
+
|
36
|
+
Ok(Validator { schema })
|
37
|
+
}
|
38
|
+
|
39
|
+
fn is_valid(&self, json: String) -> bool {
|
40
|
+
let value: serde_json::Value = serde_json::from_str(&json).unwrap();
|
41
|
+
|
42
|
+
self.schema.is_valid(&value)
|
43
|
+
}
|
44
|
+
|
45
|
+
fn validate(&self, json: String) -> Vec<String> {
|
46
|
+
let value: serde_json::Value = serde_json::from_str(&json).unwrap();
|
47
|
+
let mut errors: Vec<String> = vec![];
|
48
|
+
|
49
|
+
if let Err(validation_errors) = self.schema.validate(&value) {
|
50
|
+
for error in validation_errors {
|
51
|
+
let path = match format!("{}", error.instance_path).as_str() {
|
52
|
+
"" => "/".to_string(),
|
53
|
+
p => p.to_string(),
|
54
|
+
};
|
55
|
+
|
56
|
+
errors.push(format!("path \"{}\": {}", path, error));
|
57
|
+
}
|
58
|
+
}
|
59
|
+
|
60
|
+
errors
|
61
|
+
}
|
62
|
+
}
|
63
|
+
|
64
|
+
static JSON_PARSE_ERROR: Lazy<ExceptionClass> = Lazy::new(|ruby| {
|
65
|
+
let ex = ruby
|
66
|
+
.class_object()
|
67
|
+
.const_get::<_, RModule>("RustJSONSchema")
|
68
|
+
.unwrap()
|
69
|
+
.const_get("JSONParseError")
|
70
|
+
.unwrap();
|
71
|
+
// ensure `ex` is never garbage collected (e.g. if constant is
|
72
|
+
// redefined) and also not moved under compacting GC.
|
73
|
+
register_mark_object(ex);
|
74
|
+
ex
|
75
|
+
});
|
76
|
+
|
77
|
+
static SCHEMA_PARSE_ERROR: Lazy<ExceptionClass> = Lazy::new(|ruby| {
|
78
|
+
let ex = ruby
|
79
|
+
.class_object()
|
80
|
+
.const_get::<_, RModule>("RustJSONSchema")
|
81
|
+
.unwrap()
|
82
|
+
.const_get("SchemaParseError")
|
83
|
+
.unwrap();
|
84
|
+
// ensure `ex` is never garbage collected (e.g. if constant is
|
85
|
+
// redefined) and also not moved under compacting GC.
|
86
|
+
register_mark_object(ex);
|
87
|
+
ex
|
88
|
+
});
|
89
|
+
|
90
|
+
#[magnus::init]
|
91
|
+
fn init(ruby: &Ruby) -> Result<(), magnus::Error> {
|
92
|
+
let module = ruby.define_module("RustJSONSchema")?;
|
93
|
+
let class = module.define_class("Validator", ruby.class_object())?;
|
94
|
+
|
95
|
+
class.define_singleton_method("new", function!(Validator::new, 1))?;
|
96
|
+
|
97
|
+
class.define_method("valid?", method!(Validator::is_valid, 1))?;
|
98
|
+
class.define_method("validate", method!(Validator::validate, 1))?;
|
99
|
+
|
100
|
+
Ok(())
|
101
|
+
}
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "rust_json_schema/version"
|
4
|
+
require_relative "rust_json_schema/rust_json_schema"
|
5
|
+
|
6
|
+
module RustJSONSchema
|
7
|
+
class Error < StandardError; end
|
8
|
+
|
9
|
+
class JSONParseError < Error; end
|
10
|
+
class SchemaParseError < Error; end
|
11
|
+
end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rust_json_schema
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Taylor Thurlow
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-01-11 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Ruby wrapper for jsonschema-rs
|
14
|
+
email:
|
15
|
+
- thurlow@hey.com
|
16
|
+
executables: []
|
17
|
+
extensions:
|
18
|
+
- ext/rust_json_schema/Cargo.toml
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- ".rspec"
|
22
|
+
- ".standard.yml"
|
23
|
+
- Cargo.lock
|
24
|
+
- Cargo.toml
|
25
|
+
- LICENSE.txt
|
26
|
+
- README.md
|
27
|
+
- Rakefile
|
28
|
+
- ext/rust_json_schema/Cargo.toml
|
29
|
+
- ext/rust_json_schema/extconf.rb
|
30
|
+
- ext/rust_json_schema/src/lib.rs
|
31
|
+
- lib/rust_json_schema.rb
|
32
|
+
- lib/rust_json_schema/version.rb
|
33
|
+
- sig/rust_json_schema.rbs
|
34
|
+
homepage: https://github.com/taylorthurlow/rust_json_schema
|
35
|
+
licenses:
|
36
|
+
- MIT
|
37
|
+
metadata:
|
38
|
+
homepage_uri: https://github.com/taylorthurlow/rust_json_schema
|
39
|
+
source_code_uri: https://github.com/taylorthurlow/rust_json_schema
|
40
|
+
changelog_uri: https://github.com/taylorthurlow/rust_json_schema/releases
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '3.0'
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 3.3.11
|
55
|
+
requirements: []
|
56
|
+
rubygems_version: 3.4.19
|
57
|
+
signing_key:
|
58
|
+
specification_version: 4
|
59
|
+
summary: Ruby wrapper for jsonschema-rs
|
60
|
+
test_files: []
|