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.
Simplify Your Schema Management in Rails.
DrySchemaRails is a lightweight gem that leverages dry-schema
for defining schemas in a DRY and consistent manner across your Rails application. It encapsulates common schema definition functionalities, promoting a clean and easy-to-follow schema definition practice.
Add DrySchemaRails to your Gemfile and bundle.
gem 'dry_schema_rails'
Run
bundle install
Define Schema:
Define your schema classes with necessary validation rules in a block passed to schema
.
class UserSchema < DrySchemaRails::Base
schema do
required(:username).filled(:string)
required(:email).filled(:string, format?: /@/)
required(:age).filled(:integer, gt?: 18)
end
end
Use Schema:
Validate and sanitize parameters using defined schemas.
user_params = { username: "John", email: "john@example.com", age: 25 }
result = UserSchema.call(user_params)
puts result.success? # => true
puts result.errors # => {}
module User
class CreateValidator < DrySchemaRails::Base
params User::CreateSchema.params
rule(:username) do
key.failure('must be unique') if User.exists?(username: value)
end
rule(:email) do
key.failure('must be in internation format') unless value.end_with?('.com')
end
end
end
class UsersController < ApiController
...
schema(:create, &UserSchema.schema)
# This checks maybe in base controller for reusability
if safe_params&.failure?
render json: { errors: safe_params.errors }, status: :unprocessable_entity
end
def create
@user = User.create!(safe_params.output)
render json: @user, status: :created
end
...
end
Create a Rails application DrySchemaRailsDemo
demonstrating the usage of DrySchemaRails
in various common Rails use-cases: model validation, parameter validation in controllers, form object validation, etc.
Model and Schema:
Define User
model and UserSchema
for validating user attributes.
class User < ApplicationRecord
# your model code
end
class UserSchema < DrySchemaRails::Base
schema do
required(:username).filled(:string)
# additional validations...
end
end
Controller Validation:
Implement validations in controllers and API endpoints using schema.
class UsersController < ApplicationController
def create
validation = UserSchema.call(params.permit!.to_h)
if validation.success?
# handle success...
else
# handle failure...
end
end
end
Form Object:
Define form objects validating data using schema before processing.
class RegistrationForm
def initialize(params)
@params = params
end
def save
validation = UserSchema.call(@params)
if validation.success?
User.create(validation.output)
else
# handle errors...
end
end
end
Write tests for models, controllers, and form objects ensuring schema validations are applied and working as expected.
See LICENSE
file.
For more complex understanding you might adjust specifics to cater to the unique needs and standards of your app or team. For further details on dry-schema
, visit Dry Schema.
FAQs
Unknown package
We found that dry_schema_rails 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.