Skip to content
Open
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
18 changes: 13 additions & 5 deletions Sources/JSONSchema/JSONSchema+Enum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,20 @@ public extension JSONSchema {
/// - values: An array of possible values for this enum schema.
/// - Returns: A new ``JSONSchema`` instance that represents an enum schema.
static func `enum`(description: String? = nil, values: [EnumSchema.Value]) -> JSONSchema {
JSONSchema(
type: .enum,
let inferredType: SchemaType = {
guard let first = values.first else { return .string }
switch first {
case .string(_): return .string
case .number(_): return .number
case .integer(_): return .integer
case .boolean(_): return .boolean
case .null: return .null
}
}()
return JSONSchema(
type: inferredType,
description: description,
enumSchema: EnumSchema(
values: values
)
enumSchema: EnumSchema(values: values)
)
}
}
41 changes: 27 additions & 14 deletions Sources/JSONSchema/JSONSchema.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,19 @@
import Foundation

/// A class that represents a JSON Schema definition.
///
/// `JSONSchema` is a flexible representation of a JSON Schema. It supports multiple schema types,
/// including arrays, booleans, enums, integers, nulls, numbers, objects, and strings. The schema
/// includes detailed metadata and type-specific information.
///
/// ## Features
/// - Supports various schema types.
/// - Provides detailed descriptions for schema elements.
/// - Implements `Codable` and `Sendable` for serialization and concurrency safety.
public final class JSONSchema: Codable, Sendable {
enum SchemaType: String, Codable, Sendable {

/// Enumeration of the supported JSON Schema types.
public enum SchemaType: String, Codable, Sendable {
case array
case boolean
case `enum`
Expand All @@ -20,17 +31,20 @@ public final class JSONSchema: Codable, Sendable {
case string
}

let type: SchemaType
let description: String?
/// The type of the schema.
public let type: SchemaType

/// An optional description providing additional information about the schema.
public let description: String?

let arraySchema: ArraySchema?
let booleanSchema: BooleanSchema?
let enumSchema: EnumSchema?
let integerSchema: IntegerSchema?
let nullSchema: NullSchema?
let numberSchema: NumberSchema?
let objectSchema: ObjectSchema?
let stringSchema: StringSchema?
public let arraySchema: ArraySchema?
public let booleanSchema: BooleanSchema?
public let enumSchema: EnumSchema?
public let integerSchema: IntegerSchema?
public let nullSchema: NullSchema?
public let numberSchema: NumberSchema?
public let objectSchema: ObjectSchema?
public let stringSchema: StringSchema?

init(
type: SchemaType,
Expand Down Expand Up @@ -153,9 +167,8 @@ public final class JSONSchema: Codable, Sendable {
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)

if type != .enum {
try container.encode(type, forKey: .type)
}
// Always encode the type, including for enum
try container.encode(type, forKey: .type)

try container.encodeIfPresent(description, forKey: .description)

Expand Down