{"id":15102814,"url":"https://github.com/shopify/jsonshema-ios","last_synced_at":"2025-09-27T00:31:37.672Z","repository":{"id":54149447,"uuid":"84358515","full_name":"Shopify/jsonshema-ios","owner":"Shopify","description":"Siwft-based framework for static json schema validation in JSON-serializable models.","archived":true,"fork":false,"pushed_at":"2017-05-08T18:14:01.000Z","size":52,"stargazers_count":9,"open_issues_count":4,"forks_count":3,"subscribers_count":415,"default_branch":"master","last_synced_at":"2024-09-25T19:07:03.837Z","etag":null,"topics":["json","jsonschema","swift"],"latest_commit_sha":null,"homepage":null,"language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Shopify.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-03-08T19:32:56.000Z","updated_at":"2024-07-28T10:57:13.000Z","dependencies_parsed_at":"2022-08-13T07:40:26.366Z","dependency_job_id":null,"html_url":"https://github.com/Shopify/jsonshema-ios","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shopify%2Fjsonshema-ios","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shopify%2Fjsonshema-ios/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shopify%2Fjsonshema-ios/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shopify%2Fjsonshema-ios/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Shopify","download_url":"https://codeload.github.com/Shopify/jsonshema-ios/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":219871828,"owners_count":16554457,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["json","jsonschema","swift"],"created_at":"2024-09-25T19:07:25.658Z","updated_at":"2025-09-27T00:31:37.343Z","avatar_url":"https://github.com/Shopify.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# JSONSchema static validation\nJSONSchema is a swift framework that allows add support for json serialization to your models in a safe manner.\n\n# JSON Schema standard\nThe core of `JSONSchema` is a standard used for validation json data defined called [json schema](http://json-schema.org/). The idea behind it is defining the schema for json structure (which includes attributes names, their types and values validation rules) in a separate json document. Having json schema defined enables to run it's validation check against any arbitrary json data and tell if it matches or not.\n\n# JSONSchema overview\n`JSONSchema` framework provides a way to define json schema in a typesafe manner (as opposed to having schema defined as another json document, thus unsafe) as well it adds some basic unboxing mechanics. Framework also provides better abstraction for parsed json data (better than `[String: Any]` provided by `NSJSONSerialization`) — `JSONValue` and `JSONObject`.\n\n# Basic definitions\nFramework consists of several parts which are the following:\n\n## JSONValue\nAdds thin layer of abstraction for any arbitrary json data. Represented as `enum` defining all basic json primitives: `null`, `bool`, `string`, `number`, `array`, `object`. `JSONObject` is a convenience typealias for `[String: JSONValue]`, also has convenience initializer for any untyped dictionary that usually comes out of `NSJSONSerialization`. Since untyped dictionary can theoretically contain non-json data, `JSONObject` initializer is failable. \n\n## JSONSchema\nThis is a core of a framework. `JSONSchemaType` is starting point for providing Schema definition. It's a protocol that your custom schema type has to implement in order for the type to be used as schema definition.\n\n## SafeJSONSerializable\nDefines a mix-in interface that any model type can adopt to start supporting json de-serialization using schema validation. Types adopting this protocol must provide associated schema type and the routine to unwrap data from json data object, that is called after validation successfully completed.\n\n## Schema validation DSL\nIn order to build validation rules a set of functions is provided forming a kind of DSL that looks very closely to how validation rules are defined in json schema.\n\n\n# Usage\nSuppose you have a type representing model object like following:\n\n```swift\nstruct MyModel {\n    var boolField: Bool\n    var stringField: String\n    var arrayField: [Int]\n}\n```\n\nin order to add json schema validation to this type you adopt `SafeJSONSerializable` protocol and define schema and de-serializaion routine:\n\n```swift\nextension MyModel: SafeJSONSerializable {\n    struct Schema: JSONSchemaType {\n        enum PropertyName: String {\n            case bool_field\n            case string_field\n            case array_field\n        }\n        let required: [PropertyName] = [.bool_field]\n        \n        var properties: [PropertyName : JSONValueValidator] {\n            return [\n                .bool_field: bool(),\n                .string_field: string(\n                    length(min: 10, max: 50)\n                ),\n                .array_field: array(\n                    items(\n                        number()\n                    )\n                )\n            ]\n        }        \n    }\n\n    public static func fromValidatedJSON(json: [Schema.PropertyName : Any]) throws -\u003e MyModel {\n        let boolField = (json[.bool_field] as! Bool)\n        let stringField = json[.string_field] as? String\n        let arrayField = (json[.array_field] as? [JSONValue]).flatMap { $0.map { $0.asString() }}\n\n        \n        return MyModel(boolField: boolField,\n                    stringField: stringField,\n                    arrayField: arrayField\n        )\n    \n    }\n}\n```\n\nNotice how we unwrapping validated json data. First, we have values already unwrapped from JSONValue for primitives (except arrays and objects) as we can force cast `boolField` because it was explicitly marked as required in schema (which means it is guaranteed for the attribute to be present and be of that expected type.)\nAlso note, values in this `json` is only unwrapped on upper level. If there nested values, like array of `JSONValue`, or `JSONObject` — only container is unwrapped, and instead of `.array([JSONValue])` you will receive `[JSONValue]`.\n\nIn order to construct model object defined as described above, follow this:\n```\nlet data: [String: Any] = ... //use NSJSONSerialization here\nlet jsonData = try! JSONObject(raw: data)\nlet model = try! MyModel.fromJSON(json: jsonData)\n```\n\n\n# Validators DSL reference\nHere is the list of available DSL validators:\n\n## Type validators\nthese make sure that value matches type\n- `null`, `number`, `string`, `bool`, `object`, `array`\n\n## String validators\n- `format` matches built-in format, currently supported: `email`, `datetime`\n- `pattern` that takes regular expression to match against\n- `length` validates length of a string to match min and max values\n- `enum` checks if value is within given list\n\n## Number validators\n- `value` checks if value is within given range\n- `multipleOf` checks if value is of given multiple\n\n## Array specific validators\n- `items` take generic json value validator to check array members against\n- `length` checks that array's count of items is within range\n- `unique` checks of there are no duplicates in the array\n\n## Combinatoric validators\nLogical combination of given set of validators.\n\n- `allOf`\n- `anyOf`\n- `oneOf`\n- `noneOf`\n\n# Credits\n`JSONSchema` created by Sergey Gavrilyuk [@octogavrix](http://twitter.com/octogavrix).\n\n\n## License\n`JSONSchema` is distributed under MIT license. See LICENSE for more info.\n\n## Contributing\nFork, branch \u0026 pull request.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshopify%2Fjsonshema-ios","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshopify%2Fjsonshema-ios","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshopify%2Fjsonshema-ios/lists"}