Skip to content

Commit

Permalink
Initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
skryukov committed Sep 26, 2023
0 parents commit 63dba00
Show file tree
Hide file tree
Showing 121 changed files with 3,882 additions and 0 deletions.
50 changes: 50 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: Ruby

on:
push:
branches:
- main

pull_request:

jobs:
lint:
runs-on: ubuntu-latest
name: RuboCop Linter
env:
BUNDLE_JOBS: 4
BUNDLE_RETRY: 3
steps:
- uses: actions/checkout@v3
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: "3.2"
bundler-cache: true
- name: Run StandardRB
run: bundle exec standardrb

build:
runs-on: ubuntu-latest
name: Ruby ${{ matrix.ruby }}
env:
BUNDLE_JOBS: 4
BUNDLE_RETRY: 3
strategy:
matrix:
ruby:
- "3.2"
- "3.1"
- "3.0"
- "2.7"
- "jruby"
- "truffleruby"
steps:
- uses: actions/checkout@v3
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
- name: Run RSpec
run: bundle exec rspec
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/.bundle/
/.yardoc
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/

# rspec failure tracking
.rspec_status

Gemfile.lock
11 changes: 11 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[submodule "spec/json_schema_test_suite"]
path = spec/json_schema_test_suite
url = https://github.com/json-schema-org/JSON-Schema-Test-Suite
[submodule "data/draft-2020-12"]
path = data/draft-2020-12
url = https://github.com/json-schema-org/json-schema-spec
branch = 2020-12
[submodule "draft-2019-09"]
path = data/draft-2019-09
url = https://github.com/json-schema-org/json-schema-spec
branch = 2019-09
3 changes: 3 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--format documentation
--color
--require spec_helper
3 changes: 3 additions & 0 deletions .standard.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# For available configuration options, see:
# https://github.com/testdouble/standard
ruby_version: 2.6
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog],
and this project adheres to [Semantic Versioning].

## [Unreleased]

## [0.1.0] - 2023-09-27

### Added

- Initial implementation. ([@skryukov])

[@skryukov]: https://github.com/skryukov

[Unreleased]: https://github.com/skryukov/json_skooma/compare/v0.1.0...HEAD
[0.1.0]: https://github.com/skryukov/json_skooma/commits/v0.1.0

[Keep a Changelog]: https://keepachangelog.com/en/1.0.0/
[Semantic Versioning]: https://semver.org/spec/v2.0.0.html
12 changes: 12 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

source "https://rubygems.org"

# Specify your gem's dependencies in json_skooma.gemspec
gemspec

gem "rake", "~> 13.0"

gem "rspec", "~> 3.0"

gem "standard", "~> 1.3"
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2023 Svyatoslav Kryukov

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
107 changes: 107 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# JSONSkooma – Sugar for your JSONs

[![Gem Version](https://badge.fury.io/rb/json_skooma.svg)](https://rubygems.org/gems/json_skooma)
[![Ruby](https://github.com/skryukov/json_skooma/actions/workflows/main.yml/badge.svg)](https://github.com/skryukov/json_skooma/actions/workflows/main.yml)

JSONSkooma is a Ruby library for validating JSONs against JSON Schemas.

### Features

- Supports JSON Schema 2019-09 and 2020-12
- Supports custom dialects, vocabularies, keywords, format validators, output formatters
- Supports custom schema resolvers

<a href="https://evilmartians.com/?utm_source=json_skooma&utm_campaign=project_page">
<img src="https://evilmartians.com/badges/sponsored-by-evil-martians.svg" alt="Sponsored by Evil Martians" width="236" height="54">
</a>

## Installation

Install the gem and add to the application's Gemfile by executing:

$ bundle add json_skooma

If bundler is not being used to manage dependencies, install the gem by executing:

$ gem install json_skooma

## Usage

```ruby
require "json_skooma"

# Create a registry to store schemas, vocabularies, dialects, etc.
JSONSkooma.create_registry("2020-12", assert_formats: true)

# Load a schema
schema_hash = {
"$schema" => "https://json-schema.org/draft/2020-12/schema",
"type" => "object",
"properties" => {
"name" => {"type" => "string"},
"race" => {"enum" => %w[Nord Khajiit Argonian Breton Redguard Dunmer Altmer Bosmer Orc Imperial]},
"class" => {"type" => "string"},
"level" => {"type" => "integer", "minimum" => 1},
"equipment" => {
"type" => "array",
"items" => {"type" => "string"}
}
},
"required" => %w[name race class level]
}

schema = JSONSkooma::JSONSchema.new(schema_hash)

data_hash = {
name: "Matz",
race: "Human",
class: "Dragonborn",
level: 50,
equipment: %w[Ruby],
}

result = schema.evaluate(data_hash)

result.valid? # => false

result.output(:basic)
# {"valid"=>false,
# "errors"=>
# [{"instanceLocation"=>"",
# "keywordLocation"=>"/properties",
# "absoluteKeywordLocation"=>"urn:uuid:f477b6ca-7308-4be6-b88c-e848b9002793#/properties",
# "error"=>"Properties [\"race\"] are invalid"},
# {"instanceLocation"=>"/race",
# "keywordLocation"=>"/properties/race/enum",
# "absoluteKeywordLocation"=>"urn:uuid:f477b6ca-7308-4be6-b88c-e848b9002793#/properties/race/enum",
# "error"=>
# "The instance value \"Human\" must be equal to one of the elements in the defined enumeration: [\"Nord\", \"Khajiit\", \"Argonian\", \"Breton\", \"Redguard\", \"Dunmer\", \"Altmer\", \"Bosmer\", \"Orc\", \"Imperial\"]"}]}
```

## Alternatives

- [json_schemer](https://github.com/davishmcclurg/json_schemer) – Draft 4, 6, 7, 2019-09 and 2020-12 compliant
- [json-schema](https://github.com/voxpupuli/json-schema) – Draft 1, 2, 3, 4 and 6 compliant

## Feature plans

- Custom error messages
- EcmaScript regexp
- Short circuit errors
- IRI as schema identifiers
- Relative JSONPointer
- More unit tests

## Development

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.

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).

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/skryukov/json_skooma.

## License

The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
10 changes: 10 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

require "bundler/gem_tasks"
require "rspec/core/rake_task"

RSpec::Core::RakeTask.new(:spec)

require "standard/rake"

task default: %i[spec standard]
11 changes: 11 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require "bundler/setup"
require "json_skooma"

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.

require "irb"
IRB.start(__FILE__)
8 changes: 8 additions & 0 deletions bin/setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
set -vx

bundle install

# Do any other automated setup that you need to do here
1 change: 1 addition & 0 deletions data/draft-2019-09
Submodule draft-2019-09 added at 41014e
1 change: 1 addition & 0 deletions data/draft-2020-12
Submodule draft-2020-12 added at 769daa
33 changes: 33 additions & 0 deletions json_skooma.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

require_relative "lib/json_skooma/version"

Gem::Specification.new do |spec|
spec.name = "json_skooma"
spec.version = JSONSkooma::VERSION
spec.authors = ["Svyatoslav Kryukov"]
spec.email = ["s.g.kryukov@yandex.ru"]

spec.summary = "I bring some sugar for your JSONs."
spec.description = "I bring some sugar for your JSONs."
spec.homepage = "https://github.com/skryukov/json_skooma"
spec.license = "MIT"
spec.required_ruby_version = ">= 2.6.0"

spec.metadata = {
"bug_tracker_uri" => "#{spec.homepage}/issues",
"changelog_uri" => "#{spec.homepage}/blob/main/CHANGELOG.md",
"documentation_uri" => "#{spec.homepage}/blob/main/README.md",
"homepage_uri" => spec.homepage,
"source_code_uri" => spec.homepage,
"rubygems_mfa_required" => "true"
}

spec.files = Dir.glob("lib/**/*") + Dir.glob("data/**/{*.json,README.md}") + %w[README.md LICENSE.txt CHANGELOG.md]
spec.require_paths = ["lib"]

spec.add_runtime_dependency "zeitwerk", "~> 2.6"
spec.add_runtime_dependency "hana", "~> 1.3"
spec.add_runtime_dependency "regexp_parser", "~> 2.0"
spec.add_runtime_dependency "uri-idna", "~> 0.1"
end
39 changes: 39 additions & 0 deletions lib/json_skooma.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

require "json"
require "zeitwerk"

require_relative "json_skooma/inflector"

loader = Zeitwerk::Loader.for_gem
loader.inflector = JSONSkooma::Inflector.new
loader.setup

module JSONSkooma
DATA_DIR = File.join(__dir__, "..", "data")

class Error < StandardError; end

class << self
attr_accessor :dialects

def register_dialect(version_key, dialect)
dialects[version_key] = dialect
end

def create_registry(*schema_dialects, name: Registry::DEFAULT_NAME, assert_formats: false)
registry = Registry.new(name: name)

schema_dialects.each do |version_key|
dialects.fetch(version_key).call(registry, assert_formats: assert_formats)
end

registry
end
end

self.dialects = {}

register_dialect("2019-09", Dialects::Draft201909)
register_dialect("2020-12", Dialects::Draft202012)
end
Loading

0 comments on commit 63dba00

Please sign in to comment.