Files
assistant-storefront/app/models/concerns/json_schema_validator.rb
Liang XJ 092fb2e083
Some checks failed
Lock Threads / action (push) Has been cancelled
Mark stale issues and pull requests / stale (push) Has been cancelled
Publish Chatwoot EE docker images / build (linux/amd64, ubuntu-latest) (push) Has been cancelled
Publish Chatwoot EE docker images / build (linux/arm64, ubuntu-22.04-arm) (push) Has been cancelled
Publish Chatwoot EE docker images / merge (push) Has been cancelled
Publish Chatwoot CE docker images / build (linux/amd64, ubuntu-latest) (push) Has been cancelled
Publish Chatwoot CE docker images / build (linux/arm64, ubuntu-22.04-arm) (push) Has been cancelled
Publish Chatwoot CE docker images / merge (push) Has been cancelled
Run Chatwoot CE spec / lint-backend (push) Has been cancelled
Run Chatwoot CE spec / lint-frontend (push) Has been cancelled
Run Chatwoot CE spec / frontend-tests (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (0, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (1, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (10, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (11, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (12, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (13, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (14, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (15, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (2, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (3, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (4, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (5, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (6, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (7, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (8, 16) (push) Has been cancelled
Run Chatwoot CE spec / backend-tests (9, 16) (push) Has been cancelled
Run Linux nightly installer / nightly (push) Has been cancelled
Initial commit: Add logistics and order_detail message types
- Add Logistics component with progress tracking
- Add OrderDetail component for order information
- Support data-driven steps and actions
- Add blue color scale to widget SCSS
- Fix node overflow and progress bar rendering issues
- Add English translations for dashboard components

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-26 11:16:56 +08:00

96 lines
3.1 KiB
Ruby

# This file defines a custom validator class `JsonSchemaValidator` for validating a JSON object against a schema.
# To use this validator, define a schema as a Ruby hash and include it in the validation options when validating a model.
# The schema should define the expected structure and types of the JSON object, as well as any validation rules.
# Here's an example schema:
#
# schema = {
# 'type' => 'object',
# 'properties' => {
# 'name' => { 'type' => 'string' },
# 'age' => { 'type' => 'integer' },
# 'is_active' => { 'type' => 'boolean' },
# 'tags' => { 'type' => 'array' },
# 'address' => {
# 'type' => 'object',
# 'properties' => {
# 'street' => { 'type' => 'string' },
# 'city' => { 'type' => 'string' }
# },
# 'required' => ['street', 'city']
# }
# },
# 'required': ['name', 'age']
# }.to_json.freeze
#
# To validate a model using this schema, include the `JsonSchemaValidator` in the model's validations and pass the schema
# as an option:
#
# class MyModel < ApplicationRecord
# validates_with JsonSchemaValidator, schema: schema
# end
class JsonSchemaValidator < ActiveModel::Validator
def validate(record)
# Get the attribute resolver function from options or use a default one
attribute_resolver = options[:attribute_resolver] || ->(rec) { rec.additional_attributes }
# Resolve the JSON data to be validated
json_data = attribute_resolver.call(record)
# Get the schema to be used for validation
schema = options[:schema]
# Create a JSONSchemer instance using the schema
schemer = JSONSchemer.schema(schema)
# Validate the JSON data against the schema
validation_errors = schemer.validate(json_data)
# Add validation errors to the record with a formatted statement
validation_errors.each do |error|
format_and_append_error(error, record)
end
end
private
def format_and_append_error(error, record)
return handle_required(error, record) if error['type'] == 'required'
return handle_minimum(error, record) if error['type'] == 'minimum'
return handle_maximum(error, record) if error['type'] == 'maximum'
type = error['type'] == 'object' ? 'hash' : error['type']
handle_type(error, record, type)
end
def handle_required(error, record)
missing_values = error['details']['missing_keys']
missing_values.each do |missing|
record.errors.add(missing, 'is required')
end
end
def handle_type(error, record, expected_type)
data = get_name_from_data_pointer(error)
record.errors.add(data, "must be of type #{expected_type}")
end
def handle_minimum(error, record)
data = get_name_from_data_pointer(error)
record.errors.add(data, "must be greater than or equal to #{error['schema']['minimum']}")
end
def handle_maximum(error, record)
data = get_name_from_data_pointer(error)
record.errors.add(data, "must be less than or equal to #{error['schema']['maximum']}")
end
def get_name_from_data_pointer(error)
data = error['data_pointer']
# if data starts with a "/" remove it
data[1..] if data[0] == '/'
end
end