Skip to content

Commit ccf26e8

Browse files
authored
Change typeName to return JSON Schema type name, not capitalized (#8)
* Change typeName to return JSON Schema type name, not capitalized * Update README
1 parent 1d853b0 commit ccf26e8

File tree

3 files changed

+71
-30
lines changed

3 files changed

+71
-30
lines changed

README.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,47 @@ let schema: JSONSchema = .object(
127127
)
128128
```
129129

130+
### Schema Properties
131+
132+
Access schema metadata and type information through convenience properties:
133+
134+
```swift
135+
import JSONSchema
136+
137+
let schema: JSONSchema = .object(
138+
title: "Person",
139+
description: "A human being",
140+
properties: [
141+
"name": .string,
142+
"age": .integer
143+
]
144+
)
145+
146+
// Access metadata
147+
print(schema.typeName) // "object"
148+
print(schema.title) // "Person"
149+
print(schema.description) // "A human being"
150+
```
151+
152+
### JSON Value Compatibility
153+
154+
The library provides methods to check compatibility between JSON values and schemas:
155+
156+
```swift
157+
import JSONSchema
158+
159+
// Check if a JSON value is compatible with a schema
160+
let value: JSONValue = 42
161+
let schema: JSONSchema = .integer(minimum: 0)
162+
let isCompatible = value.isCompatible(with: schema) // true
163+
164+
// Strict vs non-strict compatibility
165+
let numberValue: JSONValue = 42
166+
let numberSchema: JSONSchema = .number()
167+
let strictCompatible = numberValue.isCompatible(with: numberSchema) // false
168+
let nonStrictCompatible = numberValue.isCompatible(with: numberSchema, strict: false) // true
169+
```
170+
130171
### Schema Serialization
131172

132173
```swift
@@ -135,7 +176,7 @@ import JSONSchema
135176
// Create a schema
136177
let schema: JSONSchema = .object(
137178
title: "Person",
138-
description: "A person schema",
179+
description: "A human being",
139180
properties: [
140181
"name": .string(),
141182
"age": .integer(minimum: 0)

Sources/JSONSchema/JSONSchema.swift

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -353,22 +353,22 @@ extension JSONSchema {
353353
/// The name of the schema type.
354354
public var typeName: String {
355355
switch self {
356-
case .object: return "Object"
357-
case .array: return "Array"
358-
case .string: return "String"
359-
case .number: return "Number"
360-
case .integer: return "Integer"
361-
case .boolean: return "Boolean"
362-
case .null: return "Null"
363-
case .reference: return "Reference"
364-
case .anyOf: return "AnyOf"
365-
case .allOf: return "AllOf"
366-
case .oneOf: return "OneOf"
367-
case .not: return "Not"
368-
case .empty: return "Empty"
369-
case .any: return "Any"
356+
case .object: return "object"
357+
case .array: return "array"
358+
case .string: return "string"
359+
case .number: return "number"
360+
case .integer: return "integer"
361+
case .boolean: return "boolean"
362+
case .null: return "null"
363+
case .reference: return "reference"
364+
case .anyOf: return "anyOf"
365+
case .allOf: return "allOf"
366+
case .oneOf: return "oneOf"
367+
case .not: return "not"
368+
case .empty: return "empty"
369+
case .any: return "any"
370370
@unknown default:
371-
return "Unknown"
371+
return "unknown"
372372
}
373373
}
374374

Tests/JSONSchemaTests/JSONSchemaTests.swift

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,59 +1028,59 @@ import Testing
10281028
@Test func testTypeName() {
10291029
// Test object schema
10301030
let objectSchema: JSONSchema = .object()
1031-
#expect(objectSchema.typeName == "Object")
1031+
#expect(objectSchema.typeName == "object")
10321032

10331033
// Test array schema
10341034
let arraySchema: JSONSchema = .array()
1035-
#expect(arraySchema.typeName == "Array")
1035+
#expect(arraySchema.typeName == "array")
10361036

10371037
// Test string schema
10381038
let stringSchema: JSONSchema = .string()
1039-
#expect(stringSchema.typeName == "String")
1039+
#expect(stringSchema.typeName == "string")
10401040

10411041
// Test number schema
10421042
let numberSchema: JSONSchema = .number()
1043-
#expect(numberSchema.typeName == "Number")
1043+
#expect(numberSchema.typeName == "number")
10441044

10451045
// Test integer schema
10461046
let integerSchema: JSONSchema = .integer()
1047-
#expect(integerSchema.typeName == "Integer")
1047+
#expect(integerSchema.typeName == "integer")
10481048

10491049
// Test boolean schema
10501050
let booleanSchema: JSONSchema = .boolean()
1051-
#expect(booleanSchema.typeName == "Boolean")
1051+
#expect(booleanSchema.typeName == "boolean")
10521052

10531053
// Test null schema
10541054
let nullSchema: JSONSchema = .null
1055-
#expect(nullSchema.typeName == "Null")
1055+
#expect(nullSchema.typeName == "null")
10561056

10571057
// Test reference schema
10581058
let referenceSchema: JSONSchema = .reference("#/definitions/Person")
1059-
#expect(referenceSchema.typeName == "Reference")
1059+
#expect(referenceSchema.typeName == "reference")
10601060

10611061
// Test anyOf schema
10621062
let anyOfSchema: JSONSchema = .anyOf([.string(), .integer()])
1063-
#expect(anyOfSchema.typeName == "AnyOf")
1063+
#expect(anyOfSchema.typeName == "anyOf")
10641064

10651065
// Test allOf schema
10661066
let allOfSchema: JSONSchema = .allOf([.string(), .integer()])
1067-
#expect(allOfSchema.typeName == "AllOf")
1067+
#expect(allOfSchema.typeName == "allOf")
10681068

10691069
// Test oneOf schema
10701070
let oneOfSchema: JSONSchema = .oneOf([.string(), .integer()])
1071-
#expect(oneOfSchema.typeName == "OneOf")
1071+
#expect(oneOfSchema.typeName == "oneOf")
10721072

10731073
// Test not schema
10741074
let notSchema: JSONSchema = .not(.string())
1075-
#expect(notSchema.typeName == "Not")
1075+
#expect(notSchema.typeName == "not")
10761076

10771077
// Test empty schema
10781078
let emptySchema: JSONSchema = .empty
1079-
#expect(emptySchema.typeName == "Empty")
1079+
#expect(emptySchema.typeName == "empty")
10801080

10811081
// Test any schema
10821082
let anySchema: JSONSchema = .any
1083-
#expect(anySchema.typeName == "Any")
1083+
#expect(anySchema.typeName == "any")
10841084
}
10851085

10861086
@Test func testTypeDefault() {

0 commit comments

Comments
 (0)