Skip to content

Commit 5ce1e1a

Browse files
refactor: rename schema title to name with format validation
- Renamed schema 'title' to 'name' to align with JSON Schema standards - Added validation for schema name to ensure it matches the pattern ^[a-zA-Z0-9_-]+$ - Updated all tests and examples to use name instead of title - Bumped version to 0.3.4 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 46eaff2 commit 5ce1e1a

9 files changed

+74
-23
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

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

5+
## [0.3.4] - 2025-03-19
6+
7+
### Changed
8+
9+
- Renamed schema `title` to `name` to align with JSON Schema standards
10+
- Added validation for schema name to ensure it matches the pattern `^[a-zA-Z0-9_-]+$`
11+
512
## [0.3.3] - 2025-03-19
613

714
### Fixed

Gemfile.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
structify (0.3.2)
4+
structify (0.3.4)
55
activesupport (>= 7.0, < 9.0)
66
attr_json (~> 2.1)
77

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class Article < ApplicationRecord
9494

9595
schema_definition do
9696
version 1
97-
title "Article Extraction"
97+
name "ArticleExtraction"
9898

9999
field :title, :string, required: true
100100
field :summary, :text

lib/structify/model.rb

+10-6
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,11 @@ def extraction_version
111111
class SchemaBuilder
112112
# @return [Class] The model class
113113
# @return [Array<Hash>] The field definitions
114-
# @return [String] The schema title
114+
# @return [String] The schema name
115115
# @return [String] The schema description
116116
# @return [Integer] The schema version
117117
# @return [Boolean] Whether thinking mode is enabled
118-
attr_reader :model, :fields, :title_str, :description_str, :version_number, :thinking_enabled
118+
attr_reader :model, :fields, :name_str, :description_str, :version_number, :thinking_enabled
119119

120120
# Initialize a new SchemaBuilder
121121
#
@@ -136,12 +136,16 @@ def thinking(enabled)
136136
@thinking_enabled = enabled
137137
end
138138

139-
# Set the schema title
139+
# Set the schema name
140140
#
141-
# @param name [String] The title
141+
# @param value [String] The name
142142
# @return [void]
143-
def title(name)
144-
@title_str = name
143+
def name(value)
144+
# Validate the name pattern (must match ^[a-zA-Z0-9_-]+$)
145+
unless value =~ /^[a-zA-Z0-9_-]+$/
146+
raise ArgumentError, "Schema name must only contain alphanumeric characters, underscores, and hyphens"
147+
end
148+
@name_str = value
145149
end
146150

147151
# Set the schema description

lib/structify/schema_serializer.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def to_json_schema
131131
end
132132

133133
{
134-
name: schema_builder.title_str,
134+
name: schema_builder.name_str,
135135
description: schema_builder.description_str,
136136
parameters: {
137137
type: "object",

lib/structify/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Structify
2-
VERSION = "0.3.3"
2+
VERSION = "0.3.4"
33
end

spec/structify/model_spec.rb

+49-9
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
describe ".schema_definition" do
2727
it "allows defining a schema with all available options" do
2828
model_class.schema_definition do
29-
title "Article Extraction"
29+
name "ArticleExtraction"
3030
description "Extract article metadata"
3131
version 2
3232

@@ -38,12 +38,52 @@
3838
expect(model_class.schema_builder).to be_a(Structify::SchemaBuilder)
3939
expect(model_class.extraction_version).to eq(2)
4040
end
41+
42+
it "validates schema name format" do
43+
# Valid names
44+
expect {
45+
model_class.schema_definition do
46+
name "ValidName"
47+
end
48+
}.not_to raise_error
49+
50+
expect {
51+
model_class.schema_definition do
52+
name "valid_name_with_underscores"
53+
end
54+
}.not_to raise_error
55+
56+
expect {
57+
model_class.schema_definition do
58+
name "valid-name-with-hyphens"
59+
end
60+
}.not_to raise_error
61+
62+
expect {
63+
model_class.schema_definition do
64+
name "valid123_with_numbers"
65+
end
66+
}.not_to raise_error
67+
68+
# Invalid names with spaces or special characters
69+
expect {
70+
model_class.schema_definition do
71+
name "Invalid Name With Spaces"
72+
end
73+
}.to raise_error(ArgumentError, /Schema name must only contain alphanumeric characters/)
74+
75+
expect {
76+
model_class.schema_definition do
77+
name "invalid!name@with#special$chars"
78+
end
79+
}.to raise_error(ArgumentError, /Schema name must only contain alphanumeric characters/)
80+
end
4181
end
4282

4383
describe ".json_schema" do
4484
before do
4585
model_class.schema_definition do
46-
title "Article Extraction"
86+
name "ArticleExtraction"
4787
description "Extract article metadata"
4888
field :title, :string, required: true
4989
field :summary, :text, description: "A brief summary"
@@ -54,7 +94,7 @@
5494
it "generates a valid JSON schema" do
5595
schema = model_class.json_schema
5696

57-
expect(schema[:name]).to eq("Article Extraction")
97+
expect(schema[:name]).to eq("ArticleExtraction")
5898
expect(schema[:description]).to eq("Extract article metadata")
5999
expect(schema[:parameters]).to be_a(Hash)
60100
expect(schema[:parameters][:required]).to eq(["title"])
@@ -69,7 +109,7 @@
69109
include Structify::Model
70110

71111
schema_definition do
72-
title "Article Extraction with Thinking"
112+
name "ArticleExtractionThinking"
73113
description "Extract article metadata with chain of thought"
74114
thinking true
75115
field :title, :string, required: true
@@ -406,7 +446,7 @@
406446

407447
it "defaults to version 1 if not specified" do
408448
model_class.schema_definition do
409-
title "Test"
449+
name "Test"
410450
end
411451

412452
expect(model_class.extraction_version).to eq(1)
@@ -422,7 +462,7 @@
422462
# Define version 1 schema
423463
schema_definition do
424464
version 1
425-
title "Article Extraction V1"
465+
name "ArticleExtractionV1"
426466

427467
field :title, :string
428468
field :category, :string
@@ -440,7 +480,7 @@
440480
# Define version 2 schema with additional fields
441481
schema_definition do
442482
version 2
443-
title "Article Extraction V2"
483+
name "ArticleExtractionV2"
444484

445485
# Fields from version 1
446486
field :title, :string, versions: 1..999
@@ -463,7 +503,7 @@
463503
# Define version 3 schema with simplified lifecycle syntax
464504
schema_definition do
465505
version 3
466-
title "Article Extraction V3"
506+
name "ArticleExtractionV3"
467507

468508
# Fields available in all versions (1..999)
469509
field :title, :string, versions: 1..999
@@ -491,7 +531,7 @@
491531

492532
schema_definition do
493533
version 4
494-
title "Simplified Versioning Example"
534+
name "SimplifiedVersioning"
495535

496536
# All of these syntaxes should work
497537
field :always_available, :string, versions: 1..999 # From v1 onward

spec/structify/schema_serializer_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
let(:schema_builder) do
1414
builder = Structify::SchemaBuilder.new(model_class)
15-
builder.title("Test Schema")
15+
builder.name("TestSchema")
1616
builder.description("Test Description")
1717
builder.field(:title, :string, required: true, description: "The title")
1818
builder.field(:category, :string, enum: ["tech", "business"])
@@ -25,7 +25,7 @@
2525
it "generates a valid JSON schema" do
2626
schema = serializer.to_json_schema
2727

28-
expect(schema[:name]).to eq("Test Schema")
28+
expect(schema[:name]).to eq("TestSchema")
2929
expect(schema[:description]).to eq("Test Description")
3030
expect(schema[:parameters]).to be_a(Hash)
3131
expect(schema[:parameters][:required]).to eq(["title"])

spec/structify_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
include Structify::Model
1414

1515
schema_definition do
16-
title "Test Schema"
16+
name "TestSchema"
1717
description "A test schema"
1818
version 1
1919

@@ -22,7 +22,7 @@
2222
end
2323

2424
expect(test_class.json_schema).to include(
25-
name: "Test Schema",
25+
name: "TestSchema",
2626
description: "A test schema",
2727
parameters: {
2828
type: "object",

0 commit comments

Comments
 (0)