Skip to content

Add localization #134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import PackageDescription
let package = Package(
name: "JSONSchema",
platforms: [
.macOS(.v10_13),
.macOS(.v10_13), .iOS(.v11)
],
products: [
.library(name: "JSONSchema", targets: ["JSONSchema"]),
Expand Down
6 changes: 5 additions & 1 deletion Sources/Applicators/anyOf.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import Foundation


func anyOf(context: Context, anyOf: Any, instance: Any, schema: [String: Any]) throws -> AnySequence<ValidationError> {
guard let anyOf = anyOf as? [Any] else {
return AnySequence(EmptyCollection())
}

if try !anyOf.contains(where: { try context.descend(instance: instance, subschema: $0).isValid }) {
let message = String(format: NSLocalizedString("%@ does not meet anyOf validation rules.", comment: ""), "\(instance)")
return AnySequence([
ValidationError(
"\(instance) does not meet anyOf validation rules.",
message,
instanceLocation: context.instanceLocation,
keywordLocation: context.keywordLocation
),
Expand Down
9 changes: 7 additions & 2 deletions Sources/Applicators/contains.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import Foundation


func contains(context: Context, contains: Any, instance: Any, schema: [String: Any]) throws -> AnySequence<ValidationError> {
guard let instance = instance as? [Any] else {
return AnySequence(EmptyCollection())
Expand Down Expand Up @@ -28,9 +31,10 @@ func contains(context: Context, contains: Any, instance: Any, schema: [String: A
return try context.descend(instance: subinstance, subschema: contains).isValid
}).count
if let max = max, containsCount > max {
let message = String(format: NSLocalizedString("%@ does not match contains + maxContains %@.", comment: ""), "\(instance)", "\(max)")
return AnySequence([
ValidationError(
"\(instance) does not match contains + maxContains \(max)",
message,
instanceLocation: context.instanceLocation,
keywordLocation: context.keywordLocation
)
Expand All @@ -41,9 +45,10 @@ func contains(context: Context, contains: Any, instance: Any, schema: [String: A
return AnySequence(EmptyCollection())
}

let message = String(format: NSLocalizedString("%@ does not match contains.", comment: ""), "\(instance)")
return AnySequence([
ValidationError(
"'\(instance) does not match contains",
message,
instanceLocation: context.instanceLocation,
keywordLocation: context.keywordLocation
)
Expand Down
6 changes: 5 additions & 1 deletion Sources/Applicators/dependencies.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import Foundation


func dependencies(context: Context, dependencies: Any, instance: Any, schema: [String: Any]) throws -> AnySequence<ValidationError> {
guard let dependencies = dependencies as? [String: Any] else {
return AnySequence(EmptyCollection())
Expand All @@ -13,9 +16,10 @@ func dependencies(context: Context, dependencies: Any, instance: Any, schema: [S
if let dependency = dependency as? [String] {
for key in dependency {
if !instance.keys.contains(key) {
let message = String(format: NSLocalizedString("'%@' is a dependency for '%@'.", comment: ""), key, property)
results.append(AnySequence([
ValidationError(
"'\(key)' is a dependency for '\(property)'",
message,
instanceLocation: context.instanceLocation,
keywordLocation: context.keywordLocation
),
Expand Down
6 changes: 5 additions & 1 deletion Sources/Applicators/not.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import Foundation


func not(context: Context, not: Any, instance: Any, schema: [String: Any]) throws -> AnySequence<ValidationError> {
guard try context.descend(instance: instance, subschema: not).isValid else {
return AnySequence(EmptyCollection())
}

let message = String(format: NSLocalizedString("'%@' does not match 'not' validation.", comment: ""), "\(instance)")
return AnySequence([
ValidationError(
"'\(instance)' does not match 'not' validation.",
message,
instanceLocation: context.instanceLocation,
keywordLocation: context.keywordLocation
)
Expand Down
6 changes: 5 additions & 1 deletion Sources/Applicators/oneOf.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import Foundation


func oneOf(context: Context, oneOf: Any, instance: Any, schema: [String: Any]) throws -> AnySequence<ValidationError> {
guard let oneOf = oneOf as? [Any] else {
return AnySequence(EmptyCollection())
}

if try oneOf.filter({ try context.descend(instance: instance, subschema: $0).isValid }).count != 1 {
let message = NSLocalizedString("Only one value from `oneOf` should be met.", comment: "")
return AnySequence([
ValidationError(
"Only one value from `oneOf` should be met",
message,
instanceLocation: context.instanceLocation,
keywordLocation: context.keywordLocation
),
Expand Down
3 changes: 2 additions & 1 deletion Sources/Applicators/patternProperties.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ func patternProperties(context: Context, patternProperties: Any, instance: Any,
results.append(try context.descend(instance: instance[key]!, subschema: schema))
}
} catch {
let message = String(format: NSLocalizedString("'%@' is not a valid regex pattern for patternProperties.", comment: ""), pattern)
return AnySequence([
ValidationError(
"[Schema] '\(pattern)' is not a valid regex pattern for patternProperties",
message,
instanceLocation: context.instanceLocation,
keywordLocation: context.keywordLocation
),
Expand Down
8 changes: 5 additions & 3 deletions Sources/Format/date.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ func validateDate(_ context: Context, _ value: String) -> AnySequence<Validation
if let regularExpression = try? NSRegularExpression(pattern: "^\\d{4}-\\d{2}-\\d{2}$", options: []) {
let range = NSRange(location: 0, length: value.utf16.count)
let result = regularExpression.matches(in: value, options: [], range: range)
if result.isEmpty {
if result.isEmpty {
let message = String(format: NSLocalizedString("'%@' is not a valid RFC 3339 formatted date.", comment: ""), value)
return AnySequence([
ValidationError(
"'\(value)' is not a valid RFC 3339 formatted date.",
message,
instanceLocation: context.instanceLocation,
keywordLocation: context.keywordLocation
)
Expand All @@ -23,9 +24,10 @@ func validateDate(_ context: Context, _ value: String) -> AnySequence<Validation
return AnySequence(EmptyCollection())
}

let message = String(format: NSLocalizedString("'%@' is not a valid RFC 3339 formatted date.", comment: ""), value)
return AnySequence([
ValidationError(
"'\(value)' is not a valid RFC 3339 formatted date.",
message,
instanceLocation: context.instanceLocation,
keywordLocation: context.keywordLocation
)
Expand Down
6 changes: 5 additions & 1 deletion Sources/Format/duration.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Foundation


/*
https://tools.ietf.org/html/rfc3339

Expand All @@ -17,6 +18,8 @@ Durations:

duration = "P" (dur-date / dur-time / dur-week)
*/


var durationExpression: NSRegularExpression = {
let second = "(\\d+S)"
let minute = "((\\d+M)\(second)?)"
Expand All @@ -41,9 +44,10 @@ func isValidDuration(_ value: String) -> Bool {

func validateDuration(_ context: Context, _ value: String) -> AnySequence<ValidationError> {
guard isValidDuration(value) else {
let message = String(format: NSLocalizedString("'%@' is not a valid duration.", comment: ""), value)
return AnySequence([
ValidationError(
"'\(value)' is not a valid duration.",
message,
instanceLocation: context.instanceLocation,
keywordLocation: context.keywordLocation
)
Expand Down
3 changes: 2 additions & 1 deletion Sources/Format/time.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,10 @@ func validateTime(_ context: Context, _ value: String) -> AnySequence<Validation
return AnySequence(EmptyCollection())
}

let message = String(format: NSLocalizedString("'%@' is not a valid RFC 3339 formatted time.", comment: ""), value)
return AnySequence([
ValidationError(
"'\(value)' is not a valid RFC 3339 formatted time.",
message,
instanceLocation: context.instanceLocation,
keywordLocation: context.keywordLocation
)
Expand Down
Binary file added Sources/Localizable.strings
Binary file not shown.
2 changes: 1 addition & 1 deletion Sources/RefResolver.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation

func urlSplitFragment(url: String) -> (String, String) {
guard let hashIndex = url.index(of: "#") else {
guard let hashIndex = url.firstIndex(of: "#") else {
return (url, "")
}

Expand Down
5 changes: 3 additions & 2 deletions Sources/Validation/const.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ func const(context: Context, const: Any, instance: Any, schema: [String: Any]) -
if isEqual(instance as! NSObject, const as! NSObject) {
return AnySequence(EmptyCollection())
}


let message = String(format: NSLocalizedString("'%@' is not equal to const '%@'", comment: ""), "\(instance)", "\(const)")
return AnySequence([
ValidationError(
"'\(instance)' is not equal to const '\(const)'",
message,
instanceLocation: context.instanceLocation,
keywordLocation: context.keywordLocation
)
Expand Down
3 changes: 2 additions & 1 deletion Sources/Validation/enum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ func `enum`(context: Context, enum: Any, instance: Any, schema: [String: Any]) -
return AnySequence(EmptyCollection())
}

let message = String(format: NSLocalizedString("'%@' is not a valid enumeration value of '%@'", comment: ""), "\(instance)", "\(`enum`)")
return AnySequence([
ValidationError(
"'\(instance)' is not a valid enumeration value of '\(`enum`)'",
message,
instanceLocation: context.instanceLocation,
keywordLocation: context.keywordLocation
)
Expand Down
11 changes: 8 additions & 3 deletions Sources/Validation/minMaxItems.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import Foundation


func validateArrayLength(_ context: Context, _ rhs: Int, comparitor: @escaping ((Int, Int) -> Bool), error: String) -> (_ value: Any) -> AnySequence<ValidationError> {
return { value in
if let value = value as? [Any] {
Expand All @@ -22,14 +25,16 @@ func minItems(context: Context, minItems: Any, instance: Any, schema: [String: A
return AnySequence(EmptyCollection())
}

return validateArrayLength(context, minItems, comparitor: >=, error: "Length of array is smaller than the minimum \(minItems)")(instance)
let message = String(format: NSLocalizedString("Length of array is smaller than the minimum %@", comment: ""), "\(minItems)")
return validateArrayLength(context, minItems, comparitor: >=, error: message)(instance)
}


func maxItems(context: Context, maxItems: Any, instance: Any, schema: [String: Any]) -> AnySequence<ValidationError> {
guard let maxItems = maxItems as? Int else {
return AnySequence(EmptyCollection())
}

return validateArrayLength(context, maxItems, comparitor: <=, error: "Length of array is greater than maximum \(maxItems)")(instance)

let message = String(format: NSLocalizedString("Length of array is greater than maximum %@", comment: ""), "\(maxItems)")
return validateArrayLength(context, maxItems, comparitor: <=, error: message)(instance)
}
9 changes: 7 additions & 2 deletions Sources/Validation/minMaxLength.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import Foundation


func validateLength(_ context: Context, _ comparitor: @escaping ((Int, Int) -> (Bool)), length: Int, error: String) -> (_ value: Any) -> AnySequence<ValidationError> {
return { value in
if let value = value as? String {
Expand All @@ -22,7 +25,8 @@ func minLength(context: Context, minLength: Any, instance: Any, schema: [String:
return AnySequence(EmptyCollection())
}

return validateLength(context, >=, length: minLength, error: "Length of string is smaller than minimum length \(minLength)")(instance)
let message = String(format: NSLocalizedString("Length of string is smaller than minimum length %@", comment: ""), "\(minLength)")
return validateLength(context, >=, length: minLength, error: message)(instance)
}


Expand All @@ -31,5 +35,6 @@ func maxLength(context: Context, maxLength: Any, instance: Any, schema: [String:
return AnySequence(EmptyCollection())
}

return validateLength(context, <=, length: maxLength, error: "Length of string is larger than max length \(maxLength)")(instance)
let message = String(format: NSLocalizedString("Length of string is larger than max length %@", comment: ""), "\(maxLength)")
return validateLength(context, <=, length: maxLength, error: message)(instance)
}
21 changes: 15 additions & 6 deletions Sources/Validation/minMaxNumber.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import Foundation


func validateNumericLength(_ context: Context, _ length: Double, comparitor: @escaping ((Double, Double) -> (Bool)), exclusiveComparitor: @escaping ((Double, Double) -> (Bool)), exclusive: Bool?, error: String) -> (_ value: Any) -> AnySequence<ValidationError> {
return { value in
if let value = value as? Double {
Expand Down Expand Up @@ -34,7 +37,8 @@ func minimumDraft4(context: Context, minimum: Any, instance: Any, schema: [Strin
return AnySequence(EmptyCollection())
}

return validateNumericLength(context, minimum, comparitor: >=, exclusiveComparitor: >, exclusive: schema["exclusiveMinimum"] as? Bool, error: "Value is lower than minimum value of \(minimum)")(instance)
let message = String(format: NSLocalizedString("Value is lower than minimum value of %@", comment: ""), "\(minimum)")
return validateNumericLength(context, minimum, comparitor: >=, exclusiveComparitor: >, exclusive: schema["exclusiveMinimum"] as? Bool, error: message)(instance)
}


Expand All @@ -43,7 +47,8 @@ func maximumDraft4(context: Context, maximum: Any, instance: Any, schema: [Strin
return AnySequence(EmptyCollection())
}

return validateNumericLength(context, maximum, comparitor: <=, exclusiveComparitor: <, exclusive: schema["exclusiveMaximum"] as? Bool, error: "Value exceeds maximum value of \(maximum)")(instance)
let message = String(format: NSLocalizedString("Value exceeds maximum value of %@", comment: ""), "\(maximum)")
return validateNumericLength(context, maximum, comparitor: <=, exclusiveComparitor: <, exclusive: schema["exclusiveMaximum"] as? Bool, error: message)(instance)
}


Expand All @@ -52,7 +57,8 @@ func minimum(context: Context, minimum: Any, instance: Any, schema: [String: Any
return AnySequence(EmptyCollection())
}

return validateNumericLength(context, minimum, comparitor: >=, exclusiveComparitor: >, exclusive: false, error: "Value is lower than minimum value of \(minimum)")(instance)
let message = String(format: NSLocalizedString("Value is lower than minimum value of %@", comment: ""), "\(minimum)")
return validateNumericLength(context, minimum, comparitor: >=, exclusiveComparitor: >, exclusive: false, error: message)(instance)
}


Expand All @@ -61,7 +67,8 @@ func maximum(context: Context, maximum: Any, instance: Any, schema: [String: Any
return AnySequence(EmptyCollection())
}

return validateNumericLength(context, maximum, comparitor: <=, exclusiveComparitor: <, exclusive: false, error: "Value exceeds maximum value of \(maximum)")(instance)
let message = String(format: NSLocalizedString("Value exceeds maximum value of %@", comment: ""), "\(maximum)")
return validateNumericLength(context, maximum, comparitor: <=, exclusiveComparitor: <, exclusive: false, error: message)(instance)
}


Expand All @@ -70,7 +77,8 @@ func exclusiveMinimum(context: Context, minimum: Any, instance: Any, schema: [St
return AnySequence(EmptyCollection())
}

return validateNumericLength(context, minimum, comparitor: >=, exclusiveComparitor: >, exclusive: true, error: "Value is lower than exclusive minimum value of \(minimum)")(instance)
let message = String(format: NSLocalizedString("Value is lower than exclusive minimum value of %@", comment: ""), "\(minimum)")
return validateNumericLength(context, minimum, comparitor: >=, exclusiveComparitor: >, exclusive: true, error: message)(instance)
}


Expand All @@ -79,5 +87,6 @@ func exclusiveMaximum(context: Context, maximum: Any, instance: Any, schema: [St
return AnySequence(EmptyCollection())
}

return validateNumericLength(context, maximum, comparitor: <=, exclusiveComparitor: <, exclusive: true, error: "Value exceeds exclusive maximum value of \(maximum)")(instance)
let message = String(format: NSLocalizedString("Value exceeds exclusive maximum value of %@", comment: ""), "\(maximum)")
return validateNumericLength(context, maximum, comparitor: <=, exclusiveComparitor: <, exclusive: true, error: message)(instance)
}
11 changes: 8 additions & 3 deletions Sources/Validation/minMaxProperties.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import Foundation


func validatePropertiesLength(_ context: Context, _ length: Int, comparitor: @escaping ((Int, Int) -> (Bool)), error: String) -> (_ value: Any) -> AnySequence<ValidationError> {
return { value in
if let value = value as? [String: Any] {
Expand All @@ -21,8 +24,9 @@ func minProperties(context: Context, minProperties: Any, instance: Any, schema:
guard let minProperties = minProperties as? Int else {
return AnySequence(EmptyCollection())
}

return validatePropertiesLength(context, minProperties, comparitor: <=, error: "Amount of properties is less than the required amount")(instance)

let message = NSLocalizedString("Amount of properties is less than the required amount", comment: "")
return validatePropertiesLength(context, minProperties, comparitor: <=, error: message)(instance)
}


Expand All @@ -31,5 +35,6 @@ func maxProperties(context: Context, maxProperties: Any, instance: Any, schema:
return AnySequence(EmptyCollection())
}

return validatePropertiesLength(context, maxProperties, comparitor: >=, error: "Amount of properties is greater than maximum permitted")(instance)
let message = NSLocalizedString("Amount of properties is greater than maximum permitted", comment: "")
return validatePropertiesLength(context, maxProperties, comparitor: >=, error: message)(instance)
}
3 changes: 2 additions & 1 deletion Sources/Validation/multipleOf.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ func multipleOf(context: Context, multipleOf: Any, instance: Any, schema: [Strin

let result = instance / multipleOf
if result != floor(result) {
let message = String(format: NSLocalizedString("%@ is not a multiple of %@", comment: ""), "\(instance)", "\(multipleOf)")
return AnySequence([
ValidationError(
"\(instance) is not a multiple of \(multipleOf)",
message,
instanceLocation: context.instanceLocation,
keywordLocation: context.keywordLocation
)
Expand Down
Loading