Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,47 @@ let schema: JSONSchema = .object(
)
```

### Schema Properties

Access schema metadata and type information through convenience properties:

```swift
import JSONSchema

let schema: JSONSchema = .object(
title: "Person",
description: "A human being",
properties: [
"name": .string,
"age": .integer
]
)

// Access metadata
print(schema.typeName) // "object"
print(schema.title) // "Person"
print(schema.description) // "A human being"
```

### JSON Value Compatibility

The library provides methods to check compatibility between JSON values and schemas:

```swift
import JSONSchema

// Check if a JSON value is compatible with a schema
let value: JSONValue = 42
let schema: JSONSchema = .integer(minimum: 0)
let isCompatible = value.isCompatible(with: schema) // true

// Strict vs non-strict compatibility
let numberValue: JSONValue = 42
let numberSchema: JSONSchema = .number()
let strictCompatible = numberValue.isCompatible(with: numberSchema) // false
let nonStrictCompatible = numberValue.isCompatible(with: numberSchema, strict: false) // true
```

### Schema Serialization

```swift
Expand All @@ -135,7 +176,7 @@ import JSONSchema
// Create a schema
let schema: JSONSchema = .object(
title: "Person",
description: "A person schema",
description: "A human being",
properties: [
"name": .string(),
"age": .integer(minimum: 0)
Expand Down
30 changes: 15 additions & 15 deletions Sources/JSONSchema/JSONSchema.swift
Original file line number Diff line number Diff line change
Expand Up @@ -353,22 +353,22 @@ extension JSONSchema {
/// The name of the schema type.
public var typeName: String {
switch self {
case .object: return "Object"
case .array: return "Array"
case .string: return "String"
case .number: return "Number"
case .integer: return "Integer"
case .boolean: return "Boolean"
case .null: return "Null"
case .reference: return "Reference"
case .anyOf: return "AnyOf"
case .allOf: return "AllOf"
case .oneOf: return "OneOf"
case .not: return "Not"
case .empty: return "Empty"
case .any: return "Any"
case .object: return "object"
case .array: return "array"
case .string: return "string"
case .number: return "number"
case .integer: return "integer"
case .boolean: return "boolean"
case .null: return "null"
case .reference: return "reference"
case .anyOf: return "anyOf"
case .allOf: return "allOf"
case .oneOf: return "oneOf"
case .not: return "not"
case .empty: return "empty"
case .any: return "any"
@unknown default:
return "Unknown"
return "unknown"
}
}

Expand Down
28 changes: 14 additions & 14 deletions Tests/JSONSchemaTests/JSONSchemaTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1028,59 +1028,59 @@ import Testing
@Test func testTypeName() {
// Test object schema
let objectSchema: JSONSchema = .object()
#expect(objectSchema.typeName == "Object")
#expect(objectSchema.typeName == "object")

// Test array schema
let arraySchema: JSONSchema = .array()
#expect(arraySchema.typeName == "Array")
#expect(arraySchema.typeName == "array")

// Test string schema
let stringSchema: JSONSchema = .string()
#expect(stringSchema.typeName == "String")
#expect(stringSchema.typeName == "string")

// Test number schema
let numberSchema: JSONSchema = .number()
#expect(numberSchema.typeName == "Number")
#expect(numberSchema.typeName == "number")

// Test integer schema
let integerSchema: JSONSchema = .integer()
#expect(integerSchema.typeName == "Integer")
#expect(integerSchema.typeName == "integer")

// Test boolean schema
let booleanSchema: JSONSchema = .boolean()
#expect(booleanSchema.typeName == "Boolean")
#expect(booleanSchema.typeName == "boolean")

// Test null schema
let nullSchema: JSONSchema = .null
#expect(nullSchema.typeName == "Null")
#expect(nullSchema.typeName == "null")

// Test reference schema
let referenceSchema: JSONSchema = .reference("#/definitions/Person")
#expect(referenceSchema.typeName == "Reference")
#expect(referenceSchema.typeName == "reference")

// Test anyOf schema
let anyOfSchema: JSONSchema = .anyOf([.string(), .integer()])
#expect(anyOfSchema.typeName == "AnyOf")
#expect(anyOfSchema.typeName == "anyOf")

// Test allOf schema
let allOfSchema: JSONSchema = .allOf([.string(), .integer()])
#expect(allOfSchema.typeName == "AllOf")
#expect(allOfSchema.typeName == "allOf")

// Test oneOf schema
let oneOfSchema: JSONSchema = .oneOf([.string(), .integer()])
#expect(oneOfSchema.typeName == "OneOf")
#expect(oneOfSchema.typeName == "oneOf")

// Test not schema
let notSchema: JSONSchema = .not(.string())
#expect(notSchema.typeName == "Not")
#expect(notSchema.typeName == "not")

// Test empty schema
let emptySchema: JSONSchema = .empty
#expect(emptySchema.typeName == "Empty")
#expect(emptySchema.typeName == "empty")

// Test any schema
let anySchema: JSONSchema = .any
#expect(anySchema.typeName == "Any")
#expect(anySchema.typeName == "any")
}

@Test func testTypeDefault() {
Expand Down