mirror of
https://github.com/discourse/discourse.git
synced 2025-03-09 14:34:35 +00:00
Why this change? This is a first pass at adding an objects validator which main's job is to validate an object against a defined schema which we will support. In this pass, we are simply validating that properties that has been marked as required are present in the object.
73 lines
1.9 KiB
Ruby
73 lines
1.9 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
RSpec.describe ThemeSettingsObjectValidator do
|
|
describe "#validate" do
|
|
it "should return the right array of error messages when properties are required but missing" do
|
|
schema = {
|
|
name: "section",
|
|
properties: {
|
|
title: {
|
|
type: "string",
|
|
required: true,
|
|
},
|
|
description: {
|
|
type: "string",
|
|
required: true,
|
|
},
|
|
links: {
|
|
type: "objects",
|
|
schema: {
|
|
name: "link",
|
|
properties: {
|
|
name: {
|
|
type: "string",
|
|
required: true,
|
|
},
|
|
child_links: {
|
|
type: "objects",
|
|
schema: {
|
|
name: "child_link",
|
|
properties: {
|
|
title: {
|
|
type: "string",
|
|
required: true,
|
|
},
|
|
not_required: {
|
|
type: "string",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
errors = described_class.new(schema:, object: {}).validate
|
|
|
|
expect(errors).to eq(title: ["must be present"], description: ["must be present"])
|
|
|
|
errors =
|
|
described_class.new(
|
|
schema: schema,
|
|
object: {
|
|
links: [{ child_links: [{}, {}] }, {}],
|
|
},
|
|
).validate
|
|
|
|
expect(errors).to eq(
|
|
title: ["must be present"],
|
|
description: ["must be present"],
|
|
links: [
|
|
{
|
|
name: ["must be present"],
|
|
child_links: [{ title: ["must be present"] }, { title: ["must be present"] }],
|
|
},
|
|
{ name: ["must be present"] },
|
|
],
|
|
)
|
|
end
|
|
end
|
|
end
|