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
149 changes: 149 additions & 0 deletions Sources/JSONSchema/JSONSchema.swift
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,155 @@
case any
}

extension JSONSchema {
/// The title of the schema, if present.
public var title: String? {
switch self {
case .object(let title, _, _, _, _, _, _, _, _),
.array(let title, _, _, _, _, _, _, _, _, _),
.string(let title, _, _, _, _, _, _, _, _, _),
.number(let title, _, _, _, _, _, _, _, _, _, _),
.integer(let title, _, _, _, _, _, _, _, _, _, _),
.boolean(let title, _, _):
return title
case .null, .reference, .anyOf, .allOf, .oneOf, .not, .empty, .any:
return nil
@unknown default:
return nil
}
}

/// The description of the schema, if present.
public var description: String? {
switch self {
case .object(_, let description, _, _, _, _, _, _, _),
.array(_, let description, _, _, _, _, _, _, _, _),
.string(_, let description, _, _, _, _, _, _, _, _),
.number(_, let description, _, _, _, _, _, _, _, _, _),
.integer(_, let description, _, _, _, _, _, _, _, _, _),
.boolean(_, let description, _):
return description
case .null, .reference, .anyOf, .allOf, .oneOf, .not, .empty, .any:
return nil
@unknown default:
return nil
}
}

/// The default value of the schema, if present.
public var `default`: JSONValue? {
switch self {
case .object(_, _, let `default`, _, _, _, _, _, _),
.array(_, _, let `default`, _, _, _, _, _, _, _),
.string(_, _, let `default`, _, _, _, _, _, _, _),
.number(_, _, let `default`, _, _, _, _, _, _, _, _),
.integer(_, _, let `default`, _, _, _, _, _, _, _, _),
.boolean(_, _, let `default`):
return `default`
case .null, .reference, .anyOf, .allOf, .oneOf, .not, .empty, .any:
return nil
@unknown default:
return nil
}
}

/// The examples of the schema, if present.
public var examples: [JSONValue]? {
switch self {
case .object(_, _, _, let examples, _, _, _, _, _),
.array(_, _, _, let examples, _, _, _, _, _, _),
.string(_, _, _, let examples, _, _, _, _, _, _),
.number(_, _, _, let examples, _, _, _, _, _, _, _),
.integer(_, _, _, let examples, _, _, _, _, _, _, _):
return examples
case .boolean, .null, .reference, .anyOf, .allOf, .oneOf, .not, .empty, .any:
return nil
@unknown default:
return nil
}
}

/// The enum values of the schema, if present.
public var `enum`: [JSONValue]? {
switch self {
case .object(_, _, _, _, let `enum`, _, _, _, _),
.array(_, _, _, _, let `enum`, _, _, _, _, _),
.string(_, _, _, _, let `enum`, _, _, _, _, _),
.number(_, _, _, _, let `enum`, _, _, _, _, _, _),
.integer(_, _, _, _, let `enum`, _, _, _, _, _, _):
return `enum`
case .boolean, .null, .reference, .anyOf, .allOf, .oneOf, .not, .empty, .any:
return nil
@unknown default:
return nil
}
}

/// The const value of the schema, if present.
public var const: JSONValue? {
switch self {
case .object(_, _, _, _, _, let const, _, _, _),
.array(_, _, _, _, _, let const, _, _, _, _),
.string(_, _, _, _, _, let const, _, _, _, _),
.number(_, _, _, _, _, let const, _, _, _, _, _),
.integer(_, _, _, _, _, let const, _, _, _, _, _):
return const
case .boolean, .null, .reference, .anyOf, .allOf, .oneOf, .not, .empty, .any:
return nil
@unknown default:
return nil
}
}

/// 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"
@unknown default:
return "Unknown"
}
}

/// The default value that the schema infers from its type.
///
/// - For objects, this property returns an empty object (`{}`).
/// - For arrays, this property returns an empty array (`[]`).
/// - For strings, this property returns an empty string (`""`).
/// - For numbers, this property returns `0.0`.
/// - For integers, this property returns `0`.
/// - For booleans, this property returns `false`.
/// - For null values, this property returns `null`.
/// - For references and composite schemas (anyOf, allOf, oneOf, not, empty, or any), this property returns `nil`.
public var typeDefault: JSONValue? {
switch self {
case .object: return .object([:])
case .array: return .array([])
case .string: return .string("")
case .number: return .double(0.0)
case .integer: return .int(0)
case .boolean: return .bool(false)
case .null: return .null
case .reference, .anyOf, .allOf, .oneOf, .not, .empty, .any:
return nil
@unknown default:
return nil
}
}
}

extension JSONSchema: Codable {
private enum CodingKeys: String, CodingKey {
// Any
Expand Down
Loading