Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
ActiveJsonSchema is a Ruby gem that extends ActiveRecord models to generate JSON Schema representations. It provides an easy way to create JSON Schema definitions for your ActiveRecord models, including support for associations.
Add this line to your application's Gemfile:
gem "active_json_schema"
And then execute:
$ bundle install
Or install it yourself as:
$ gem install active_json_schema
To use ActiveJsonSchema in your ActiveRecord models, include the ActiveJsonSchema::ToJsonSchema
module:
class User < ActiveRecord::Base
include ActiveJsonSchema::ToJsonSchema
has_many :posts
end
class Post < ActiveRecord::Base
include ActiveJsonSchema::ToJsonSchema
belongs_to :user
end
Now you can generate JSON Schema for your models:
user_schema = User.to_json_schema
puts user_schema
This will output a JSON Schema representation of the User model, including all its attributes.
You can customize the schema generation by passing options to the to_json_schema
method:
# Generate schema for specific attributes only
user_schema = User.to_json_schema(only: %w[name email])
# Include associations in the schema
user_schema = User.to_json_schema(associations: %w[posts])
# Customize association options
user_schema = User.to_json_schema(associations: { posts: { only: %w[title content] } })
# Allow additional properties
user_schema = User.to_json_schema(additional_properties: true)
ActiveJsonSchema supports generating schemas for associated models:
class User < ActiveRecord::Base
include ActiveJsonSchema::ToJsonSchema
has_many :posts
end
user_schema = User.to_json_schema(associations: %w[posts])
This will include a posts_attributes
property in the User schema, referencing the Post schema definition.
You can override the default to_json_schema
method in your models for more control:
class Post < ActiveRecord::Base
include ActiveJsonSchema::ToJsonSchema
def self.to_json_schema(only: %w[title content], **)
super
end
end
This example will always include only the 'title' and 'content' attributes in the Post schema unless specified otherwise.
Here are some more examples to illustrate the usage:
# Generate schema for User model with all attributes
User.to_json_schema
# Generate schema for User model with specific attributes and associations
User.to_json_schema(only: %w[name email], associations: %w[posts])
# Generate schema for Post model with customized association
Post.to_json_schema(associations: { user: { only: %w[name] } })
# Generate schema with nested associations
User.to_json_schema(associations: { posts: { associations: %w[comments] } })
After checking out the repo, run bin/setup
to install dependencies. Then, run rake test
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
.
Bug reports and pull requests are welcome on GitHub at https://github.com/pingortle/active_json_schema. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
The gem is available as open source under the terms of the MIT License.
FAQs
Unknown package
We found that active_json_schema demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.