项目作者: sebaoliveri

项目描述 :
Validate JSONs against json schemas in Scala
高级语言: Scala
项目地址: git://github.com/sebaoliveri/json-schema-validation.git
创建时间: 2020-10-09T17:53:33Z
项目社区:https://github.com/sebaoliveri/json-schema-validation

开源协议:MIT License

下载


json-schema-validation

Validate JSONs against json schemas

A fairly complete (not total) implementation of json schema spec

Can be trustely used in Prod, just check all keywords described in your schema are supported by this Lib.

Usage

  1. val jsonSchema =
  2. Json.parse(
  3. """
  4. {
  5. "$id": "https://example.com/person.schema.json",
  6. "$schema": "http://json-schema.org/draft-07/schema#",
  7. "title": "Person",
  8. "type": "object",
  9. "properties": {
  10. "firstName": {
  11. "type": "string",
  12. "description": "The person's first name."
  13. },
  14. "lastName": {
  15. "type": "string",
  16. "description": "The person's last name."
  17. },
  18. "age": {
  19. "description": "Age in years which must be equal to or greater than zero.",
  20. "type": "integer",
  21. "minimum": 0
  22. }
  23. },
  24. "required": ["lastName"]
  25. }
  26. """)
  27. val person =
  28. Json.parse(
  29. """{
  30. "firstName": "John",
  31. "age": -1
  32. }""")
  33. JsValidation.from(jsonSchema).appliedTo(person).matches {
  34. case AssertionSuccessfulResult(_) => fail()
  35. case AssertionFailureResult(errors) => errors should be(
  36. List(
  37. "Property Person.lastName is missing",
  38. "Person.age is smaller than required minimum value of 0"))
  39. }