### Add core scala-jsonschema dependency to SBT Source: https://github.com/andyglow/scala-jsonschema/blob/master/src/main/paradox/getting-started.md This snippet shows how to add the main `scala-jsonschema` library as a required dependency in an SBT build file. This is the primary dependency needed to use the library's core functionalities. ```Scala libraryDependencies += "com.github.andyglow" %% "scala-jsonschema" % // <-- required ``` -------------------------------- ### Add optional scala-jsonschema modules to SBT Source: https://github.com/andyglow/scala-jsonschema/blob/master/src/main/paradox/getting-started.md This snippet demonstrates how to include various optional modules for `scala-jsonschema` in an SBT build. These modules provide support for different JSON libraries (Play, Spray, Circe, json4s, ujson), Joda-Time, Cats, Refined, and a zero-dependency parser, allowing for extended functionality based on project needs. ```Scala libraryDependencies ++= Seq( "com.github.andyglow" %% "scala-jsonschema-core" % , // <-- transitive "com.github.andyglow" %% "scala-jsonschema-macros" % % Provided, // <-- transitive // json bridge. pick one "com.github.andyglow" %% "scala-jsonschema-play-json" % , // <-- optional "com.github.andyglow" %% "scala-jsonschema-spray-json" % , // <-- optional "com.github.andyglow" %% "scala-jsonschema-circe-json" % , // <-- optional "com.github.andyglow" %% "scala-jsonschema-json4s-json" % , // <-- optional "com.github.andyglow" %% "scala-jsonschema-ujson" % , // <-- optional // joda-time support "com.github.andyglow" %% "scala-jsonschema-joda-time" % , // <-- optional // cats support "com.github.andyglow" %% "scala-jsonschema-cats" % , // <-- optional // refined support "com.github.andyglow" %% "scala-jsonschema-refined" % , // <-- optional // zero-dependency json and jsonschema parser "com.github.andyglow" %% "scala-jsonschema-parser" % // <-- optional ) ``` -------------------------------- ### Generate JSON Schema using Circe Source: https://github.com/andyglow/scala-jsonschema/blob/master/README.md Example of converting a generated JSON Schema to Circe's `Json` format using the `AsCirce` support. This facilitates integration with applications built on Circe. ```scala import com.github.andyglow.jsonschema.AsCirce._ import json.schema.Version._ import io.circe._ case class Foo(name: String) val fooSchema: Json = Json.schema[Foo].asCirce(Draft04()) ``` -------------------------------- ### Enable Enumeratum Integration for JSON Schema Source: https://github.com/andyglow/scala-jsonschema/blob/master/README.md Shows the necessary import to enable `enumeratum` integration with the JSON Schema library. This allows for schema generation from `enumeratum` enums. Further examples demonstrating specific `enumeratum` features are pending. ```scala import com.github.andyglow.jsonschema.EnumeratumSupport._ ``` -------------------------------- ### Enable Cats Integration for JSON Schema Source: https://github.com/andyglow/scala-jsonschema/blob/master/README.md Shows the necessary import to enable `cats` integration with the JSON Schema library. This import makes `cats` type classes available for schema derivation. Further examples demonstrating specific `cats` features are pending. ```scala import com.github.andyglow.jsonschema.CatsSupport._ ``` -------------------------------- ### Generate JSON Schema using uJson Source: https://github.com/andyglow/scala-jsonschema/blob/master/README.md Example of converting a generated JSON Schema to uJson's `ujson.Value` format using the `AsU` support. This enables integration with applications utilizing uJson. ```scala import com.github.andyglow.jsonschema.AsU._ import json.schema.Version._ case class Foo(name: String) val fooSchema: ujson.Value = Json.schema[Foo].asU(Draft04()) ``` -------------------------------- ### Enable Refined Types Integration for JSON Schema Source: https://github.com/andyglow/scala-jsonschema/blob/master/README.md Shows the necessary import to enable `refined` types integration with the JSON Schema library. This allows for schema generation that respects `refined` type constraints. Further examples demonstrating specific `refined` features are pending. ```scala import com.github.andyglow.jsonschema.RefinedSupport._ ``` -------------------------------- ### Generate JSON Schema using Spray Json Source: https://github.com/andyglow/scala-jsonschema/blob/master/README.md Example of converting a generated JSON Schema to Spray Json's `JsValue` format using the `AsSpray` support. This enables compatibility with applications using Spray Json. ```scala import com.github.andyglow.jsonschema.AsSpray._ import json.schema.Version._ import spray.json._ case class Foo(name: String) val fooSchema: JsValue = Json.schema[Foo].asSpray(Draft04()) ``` -------------------------------- ### Generate JSON Schema using Json4s Source: https://github.com/andyglow/scala-jsonschema/blob/master/README.md Example of converting a generated JSON Schema to Json4s's `JValue` format using the `AsJson4s` support. This allows for interoperability with Json4s-based projects. ```scala import com.github.andyglow.jsonschema.AsJson4s._ import json.schema.Version._ import org.json4s.JsonAST._ case class Foo(name: String) val fooSchema: JValue = Json.schema[Foo].asJson4s(Draft04()) ``` -------------------------------- ### Generate JSON Schema using Play Json Source: https://github.com/andyglow/scala-jsonschema/blob/master/README.md Example of converting a generated JSON Schema to Play Json's `JsValue` format using the `AsPlay` support. This allows seamless integration with Play Framework applications. ```scala import com.github.andyglow.jsonschema.AsPlay._ import json.schema.Version._ import play.api.libs.json._ case class Foo(name: String) val fooSchema: JsValue = Json.schema[Foo].asPlay(Draft04()) ``` -------------------------------- ### Generate and View In-Lined JSON Schema Source: https://github.com/andyglow/scala-jsonschema/blob/master/src/main/paradox/overview.md This example demonstrates how to generate an 'in-lined' JSON Schema from a Scala class (`Person`) using the `json` library. In this mode, the generated schema embeds all type information directly without separate `definitions`. The second part of the snippet shows the resulting JSON Schema, illustrating how Scala types like case classes, sealed traits, options, and collections are mapped to JSON Schema constructs. ```scala import json._ val personSchema: json.Schema[Person] = Json.schema[Person] ``` ```json { "$schema": "http://json-schema.org/draft-04/schema#", "type": "object", "additionalProperties": false, "properties": { "middleName": { "type": "string" }, "cars": { "type": "array", "items": { "type": "object", "additionalProperties": false, "properties": { "name": { "type": "string" }, "manufacturer": { "type": "object", "additionalProperties": false, "properties": { "name": { "type": "string" } }, "required": [ "name" ] } }, "required": [ "name", "manufacturer" ] } }, "company": { "type": "object", "additionalProperties": false, "properties": { "name": { "type": "string" } }, "required": [ "name" ] }, "lastName": { "type": "string" }, "firstName": { "type": "string" }, "birthDay": { "type": "string", "format": "date-time" }, "gender": { "type": "string", "enum": [ "Male", "Female" ] } }, "required": [ "company", "lastName", "birthDay", "gender", "firstName", "cars" ] } ``` -------------------------------- ### Define Scala Data Structures for JSON Schema Generation Source: https://github.com/andyglow/scala-jsonschema/blob/master/src/main/paradox/overview.md This snippet defines a set of Scala case classes and a sealed trait enum (`Gender`) that serve as the source data structures for generating JSON Schema. It includes examples of nested objects (`Company`, `Car`), optional fields (`middleName`), date-time types (`LocalDateTime`), and collections (`Seq[Car]`). These structures are used as input for the schema generation process. ```scala sealed trait Gender object Gender { case object Male extends Gender case object Female extends Gender } case class Company(name: String) case class Car(name: String, manufacturer: Company) case class Person( firstName: String, middleName: Option[String], lastName: String, gender: Gender, birthDay: java.time.LocalDateTime, company: Company, cars: Seq[Car]) ``` -------------------------------- ### Define Scala Data Structures for JSON Schema Generation Example Source: https://github.com/andyglow/scala-jsonschema/blob/master/README.md This Scala code snippet defines a set of interconnected data structures, including a sealed trait for `Gender` (representing an enum), a `Company` case class, a `Car` case class referencing `Company`, and a `Person` case class that aggregates various types, including `Option`, `java.time.LocalDateTime`, and `Seq` of `Car`. These structures demonstrate how complex domain models can be represented in Scala for subsequent JSON schema generation. ```scala sealed trait Gender object Gender { case object Male extends Gender case object Female extends Gender } case class Company(name: String) case class Car(name: String, manufacturer: Company) case class Person( firstName: String, middleName: Option[String], lastName: String, gender: Gender, birthDay: java.time.LocalDateTime, company: Company, cars: Seq[Car]) ``` -------------------------------- ### Generate JSON Schema with External Configuration Source: https://github.com/andyglow/scala-jsonschema/blob/master/README.md Illustrates how to provide schema documentation (title and description for the class, and descriptions for fields) separately using a configuration-based approach. This method allows models to remain concise and is suitable when models are defined in separate modules or external libraries. ```scala import json._ case class Model(a: String, b: Int) val schema = Json.objectSchema[Model]( "a" -> "A Param", "b" -> "B Param" ) .withDescription("My perfect class") .withTitle("A Title") ``` ```json { "$schema": "http://json-schema.org/draft-04/schema#", "description": "My perfect class", "title": "A Title", "type": "object", "additionalProperties": false, "properties": { "a": { "type": "string", "description": "A Param" }, "b": { "type": "integer", "description": "B Param" } }, "required": [ "a", "b" ] } ``` -------------------------------- ### Generate JSON Schema with Circe Source: https://github.com/andyglow/scala-jsonschema/blob/master/src/main/paradox/modules/json.md This snippet demonstrates how to generate a JSON schema for a Scala case class using the `scala-jsonschema` library's integration with Circe. It imports necessary components and defines a schema for a simple `Foo` case class, returning an `io.circe.Json`. ```Scala import com.github.andyglow.jsonschema.AsCirce._ import json.schema.Version._ import io.circe._ case class Foo(name: String) val fooSchema: Json = Json.schema[Foo].asCirce(Draft04()) ``` -------------------------------- ### Generate JSON Schema with Scala Annotations Source: https://github.com/andyglow/scala-jsonschema/blob/master/README.md Demonstrates how to use `@title` and `@description` annotations directly on Scala case classes and their fields to embed documentation (title and description) into the generated JSON schema. This method integrates documentation directly within the model definition. ```scala import json._ import json.schema._ @title("A Title") @description("My perfect class") case class Model( @description("A Param") a: String, @description("B Param") b: Int) val schema = Json.objectSchema[Model]() ``` ```json { "$schema": "http://json-schema.org/draft-04/schema#", "description": "My perfect class", "title": "A Title", "type": "object", "additionalProperties": false, "properties": { "a": { "type": "string", "description": "A Param" }, "b": { "type": "integer", "description": "B Param" } }, "required": [ "a", "b" ] } ``` -------------------------------- ### Generate JSON Schema for Joda Time Instant in Scala Source: https://github.com/andyglow/scala-jsonschema/blob/master/src/main/paradox/modules/joda-time.md This Scala code snippet illustrates how to define a case class `Event` that incorporates a `org.joda.time.Instant` field. It then uses the `scala-jsonschema` library, along with `JodaTimeSupport`, to automatically generate the JSON Schema for this class. The output shows the schema generation process. ```scala import com.github.andyglow.jsonschema.JodaTimeSupport._ import org.joda.time._ case class Event(id: String, timestamp: Instant) val eventSchema: Schema[Event] = Json.schema[Event] println(JsonFormatter.format(AsValue.schema(eventSchema))) ``` -------------------------------- ### Generate JSON Schema with Spray Json Source: https://github.com/andyglow/scala-jsonschema/blob/master/src/main/paradox/modules/json.md This snippet demonstrates how to generate a JSON schema for a Scala case class using the `scala-jsonschema` library's integration with Spray Json. It imports necessary components and defines a schema for a simple `Foo` case class, returning a `spray.json.JsValue`. ```Scala import com.github.andyglow.jsonschema.AsSpray._ import json.schema.Version._ import spray.json._ case class Foo(name: String) val fooSchema: JsValue = Json.schema[Foo].asSpray(Draft04()) ``` -------------------------------- ### Generate JSON Schema with Play Json Source: https://github.com/andyglow/scala-jsonschema/blob/master/src/main/paradox/modules/json.md This snippet demonstrates how to generate a JSON schema for a Scala case class using the `scala-jsonschema` library's integration with Play Json. It imports necessary components and defines a schema for a simple `Foo` case class, returning a `play.api.libs.json.JsValue`. ```Scala import com.github.andyglow.jsonschema.AsPlay._ import json.schema.Version._ import play.api.libs.json._ case class Foo(name: String) val fooSchema: JsValue = Json.schema[Foo].asPlay(Draft04()) ``` -------------------------------- ### Generate JSON Schema with uJson Source: https://github.com/andyglow/scala-jsonschema/blob/master/src/main/paradox/modules/json.md This snippet demonstrates how to generate a JSON schema for a Scala case class using the `scala-jsonschema` library's integration with uJson. It imports necessary components and defines a schema for a simple `Foo` case class, returning a `ujson.Value`. ```Scala import com.github.andyglow.jsonschema.AsU._ import json.schema.Version._ case class Foo(name: String) val fooSchema: ujson.Value = Json.schema[Foo].asU(Draft04()) ``` -------------------------------- ### Generate JSON Schema with Json4s Source: https://github.com/andyglow/scala-jsonschema/blob/master/src/main/paradox/modules/json.md This snippet demonstrates how to generate a JSON schema for a Scala case class using the `scala-jsonschema` library's integration with Json4s. It imports necessary components and defines a schema for a simple `Foo` case class, returning an `org.json4s.JsonAST.JValue`. ```Scala import com.github.andyglow.jsonschema.AsJson4s._ import json.schema.Version._ import org.json4s.JsonAST._ case class Foo(name: String) val fooSchema: JValue = Json.schema[Foo].asJson4s(Draft04()) ``` -------------------------------- ### Generated JSON Schema for Joda Time Instant Source: https://github.com/andyglow/scala-jsonschema/blob/master/src/main/paradox/modules/joda-time.md This JSON snippet represents the schema automatically generated by the preceding Scala code for the `Event` case class. It specifically highlights how `org.joda.time.Instant` is mapped within the JSON Schema, appearing as a string type with a `date-time` format in the `definitions` section. ```json { "$schema": "http://json-schema.org/draft-04/schema#", "type": "object", "additionalProperties": false, "properties": { "id": { "type": "string" }, "timestamp": { "$ref": "#/definitions/org.joda.time.Instant" } }, "required": [ "id", "timestamp" ], "definitions": { "org.joda.time.Instant": { "type": "string", "format": "date-time" } } } ``` -------------------------------- ### Add Optional Scala JSON Schema Module SBT Dependencies Source: https://github.com/andyglow/scala-jsonschema/blob/master/README.md This snippet provides a comprehensive list of optional SBT dependencies for integrating `scala-jsonschema` with various JSON libraries (Play, Spray, Circe, Json4s, ujson), and adding support for Joda-Time, Cats, Refined, and Enumeratum. It also includes a zero-dependency JSON and JSON Schema parser module. Select the modules relevant to your project's specific needs. ```scala libraryDependencies ++= Seq( "com.github.andyglow" %% "scala-jsonschema-core" % , // <-- transitive "com.github.andyglow" %% "scala-jsonschema-macros" % % Provided, // <-- transitive // json bridge. pick one "com.github.andyglow" %% "scala-jsonschema-play-json" % , // <-- optional "com.github.andyglow" %% "scala-jsonschema-spray-json" % , // <-- optional "com.github.andyglow" %% "scala-jsonschema-circe-json" % , // <-- optional "com.github.andyglow" %% "scala-jsonschema-json4s-json" % , // <-- optional "com.github.andyglow" %% "scala-jsonschema-ujson" % , // <-- optional // joda-time support "com.github.andyglow" %% "scala-jsonschema-joda-time" % , // <-- optional // cats support "com.github.andyglow" %% "scala-jsonschema-cats" % , // <-- optional // refined support "com.github.andyglow" %% "scala-jsonschema-refined" % , // <-- optional // enumeratum support "com.github.andyglow" %% "scala-jsonschema-enumeratum" % , // <-- optional // zero-dependency json and jsonschema parser "com.github.andyglow" %% "scala-jsonschema-parser" % // <-- optional ) ``` -------------------------------- ### Generate Default JSON Schema from Scala Types Source: https://github.com/andyglow/scala-jsonschema/blob/master/README.md Demonstrates how to automatically generate JSON schemas for basic Scala types like String and Array[String]. The generated JSON shows the default naming convention for definitions, typically using the fully qualified class name. ```scala import json._ implicit val someStrSchema: json.Schema[String] = Json.schema[String] implicit val someArrSchema: json.Schema[Array[String]] = Json.schema[Array[String]] println(JsonFormatter.format(AsValue.schema(someArrSchema))) ``` ```json { "$schema": "http://json-schema.org/draft-04/schema#", "type": "array", "items": { "$ref": "#/definitions/java.lang.String" }, "definitions": { "java.lang.String": { "type": "string" } } } ``` -------------------------------- ### Generate JSON Schema from Scaladoc Comments Source: https://github.com/andyglow/scala-jsonschema/blob/master/README.md Shows how Scala-JsonSchema can infer descriptions for case classes and their fields directly from standard Scaladoc comments. This approach reuses existing documentation but has limitations, such as requiring model classes to be in the same module and potentially needing non-incremental builds. ```scala import json._ /** My perfect class * * @param a A Param * @param b B Param */ case class Model(a: String, b: Int) val schema = Json.objectSchema[Model]() ``` ```json { "$schema": "http://json-schema.org/draft-04/schema#", "description": "My perfect class", "type": "object", "additionalProperties": false, "properties": { "a": { "type": "string", "description": "A Param" }, "b": { "type": "integer", "description": "B Param" } }, "required": [ "a", "b" ] } ``` -------------------------------- ### Generate JSON Schema for Joda Time Instant Source: https://github.com/andyglow/scala-jsonschema/blob/master/README.md Demonstrates how to integrate Joda Time `Instant` into JSON Schema generation using `JodaTimeSupport`, resulting in a `date-time` format for the timestamp field. This ensures proper schema validation for temporal data. ```scala import com.github.andyglow.jsonschema.JodaTimeSupport._ import org.joda.time._ case class Event(id: String, timestamp: Instant) val eventSchema: Schema[Event] = Json.schema[Event] println(JsonFormatter.format(AsValue.schema(eventSchema))) ``` ```json { "$schema": "http://json-schema.org/draft-04/schema#", "type": "object", "additionalProperties": false, "properties": { "id": { "type": "string" }, "timestamp": { "$ref": "#/definitions/org.joda.time.Instant" } }, "required": [ "id", "timestamp" ], "definitions": { "org.joda.time.Instant": { "type": "string", "format": "date-time" } } } ``` -------------------------------- ### Toggle Between Free and Strict Object Schemas Source: https://github.com/andyglow/scala-jsonschema/blob/master/README.md Demonstrates the API for converting a generated object schema to a 'free' form (allowing additional properties) and back to a 'strict' form (disallowing additional properties). This provides flexibility in defining the strictness of object schemas. ```scala case class Person(name: String, age: Int) val personSchema = Json.objectSchema[Person] val freePersonSchema = personSchema.free val strictPersonSchema = freePersonSchema.strict strictPersonSchema == personSchema // equal ``` -------------------------------- ### Generate Regular JSON Schema with Definitions in Scala Source: https://github.com/andyglow/scala-jsonschema/blob/master/README.md This snippet demonstrates how to generate a JSON schema in 'regular' mode using the `scala-jsonschema` library. In this mode, separate `definitions` are created for reusable types (like `Gender`, `Company`, `Car`), and the main schema references these definitions using `$ref`. This approach promotes modularity, reusability, and can lead to more concise top-level schemas, especially for complex data models. ```scala import json._ implicit val genderSchema: json.Schema[Gender] = Json.schema[Gender] implicit val companySchema: json.Schema[Company] = Json.schema[Company] implicit val carSchema: json.Schema[Car] = Json.schema[Car] implicit val personSchema: json.Schema[Person] = Json.schema[Person] ``` ```json { "$schema": "http://json-schema.org/draft-04/schema#", "type": "object", "additionalProperties": false, "properties": { "middleName": { "type": "string" }, "cars": { "type": "array", "items": { "$ref": "#/definitions/com.github.andyglow.jsonschema.ExampleMsg.Car" } }, "company": { "$ref": "#/definitions/com.github.andyglow.jsonschema.ExampleMsg.Company" }, "lastName": { "type": "string" }, "firstName": { "type": "string" }, "birthDay": { "type": "string", "format": "date-time" }, "gender": { "$ref": "#/definitions/com.github.andyglow.jsonschema.ExampleMsg.Gender" } }, "required": [ "company", "lastName", "birthDay", "gender", "firstName", "cars" ], "definitions": { "com.github.andyglow.jsonschema.ExampleMsg.Company": { "type": "object", "additionalProperties": false, "properties": { "name": { "type": "string" } }, "required": [ "name" ] }, "com.github.andyglow.jsonschema.ExampleMsg.Car": { "type": "object", "additionalProperties": false, "properties": { "name": { "type": "string" }, "manufacturer": { "$ref": "#/definitions/com.github.andyglow.jsonschema.ExampleMsg.Company" } }, "required": [ "name", "manufacturer" ] }, "com.github.andyglow.jsonschema.ExampleMsg.Gender": { "type": "string", "enum": [ "Male", "Female" ] } } } ``` -------------------------------- ### Add Core Scala JSON Schema SBT Dependency Source: https://github.com/andyglow/scala-jsonschema/blob/master/README.md This snippet demonstrates how to include the primary `scala-jsonschema` library in your Scala project's `build.sbt` file. This dependency is essential for utilizing the JSON Schema generation capabilities. ```scala libraryDependencies += "com.github.andyglow" %% "scala-jsonschema" % // <-- required ``` -------------------------------- ### Apply JSON Schema Validation Rules Source: https://github.com/andyglow/scala-jsonschema/blob/master/README.md Demonstrates how to incorporate JSON Schema validation rules, such as `pattern`, directly into the generated schema definitions. This allows for enforcing data constraints like format or length on specific fields within the schema. ```scala import json._ import json.Validation._ implicit val vb = ValidationBound.mk[UserId, String] implicit val userIdSchema: json.Schema[UserId] = Json.schema[UserId].toDefinition("userId") withValidation ( `pattern` := "[a-f\\d]{16}" ) ``` ```json { "userId": { "type": "string", "pattern": "[a-f\\d]{16}" } } ``` -------------------------------- ### Generate Free-form JSON Object Schemas Source: https://github.com/andyglow/scala-jsonschema/blob/master/README.md Illustrates how to create a 'free' JSON object schema, which includes `"additionalProperties": true`, allowing for flexible or unknown JSON structures. This is particularly useful when a model contains fields like `JsObject` that can hold arbitrary key-value pairs. ```json { "type": "object", "additionalProperties": true } ``` ```scala import play.api.libs.json._ // model case class Payload(id: String, name: String, metadata: JsObject) // metadata schema implicit val metaSchema: json.Schema[JsObject] = json.Schema.`object`.Free[JsObject]() // or alternatively define a metadata Predef in case you need this to not go to definition section of json-schema // implicit val metaPredef: json.schema.Predef[JsObject] = json.schema.Predef(json.Schema.`object`.Free[JsObject]()) // payload schema val payloadSchema: json.Schema[Payload] = Json.schema[Payload] ``` -------------------------------- ### Supported Data Types for Scala JSON Schema Generation Source: https://github.com/andyglow/scala-jsonschema/blob/master/README.md This section lists the various data types and structures that the `scala-jsonschema` library supports for JSON schema generation, including built-in types, standard Java date/time formats, and types from integrated modules like JodaTime, Cats, Refined, and Enumeratum. It also covers standard Scala collections, sealed traits, case classes, and value classes, detailing how the library extends its capabilities through external dependencies. ```APIDOC Types supported out of the box: - Boolean - Numeric: - Short - Int - Char - Double - Float - Long - BigInt - BigDecimal - String - Date Time: - java.util.Date - java.sql.Timestamp - java.time.Instant - java.time.LocalDateTime - java.sql.Date - java.time.LocalDate - java.sql.Time - java.time.LocalTime - with JodaTime module imported: - org.joda.time.Instant - org.joda.time.DateTime - org.joda.time.LocalDateTime - org.joda.time.LocalDate - org.joda.time.LocalTime - with Cats module imported: - cats.data.NonEmptyList - cats.data.NonEmptyVector - cats.data.NonEmptySet - cats.data.NonEmptyChain - cats.data.NonEmptyMap - cats.data.NonEmptyStream (for scala 2.11, 2.12) - cats.data.NonEmptyLazyList (for scala 2.13) - cats.data.OneAnd - with Refined module imported (refine original types): - boolean: - eu.timepit.refined.boolean.And - eu.timepit.refined.boolean.Or - eu.timepit.refined.boolean.Not - string: - eu.timepit.refined.collection.Size - eu.timepit.refined.collection.MinSize - eu.timepit.refined.collection.MaxSize - eu.timepit.refined.collection.Empty - eu.timepit.refined.collection.NonEmpty - eu.timepit.refined.string.Uuid - eu.timepit.refined.string.Uri - eu.timepit.refined.string.Url - eu.timepit.refined.string.IPv4 - eu.timepit.refined.string.IPv6 - eu.timepit.refined.string.Xml - eu.timepit.refined.string.StartsWith - eu.timepit.refined.string.EndsWith - eu.timepit.refined.string.MatchesRegex - eu.timepit.refined.string.Trimmed - number: - eu.timepit.refined.numeric.Positive - eu.timepit.refined.numeric.Negative - eu.timepit.refined.numeric.NonPositive - eu.timepit.refined.numeric.NonNegative - eu.timepit.refined.numeric.Greather - eu.timepit.refined.numeric.Less - eu.timepit.refined.numeric.GreaterEqual - eu.timepit.refined.numeric.LessEqual - eu.timepit.refined.numeric.Divisable - collection: - eu.timepit.refined.collection.Size - eu.timepit.refined.collection.MinSize - eu.timepit.refined.collection.MaxSize - eu.timepit.refined.collection.Empty - eu.timepit.refined.collection.NonEmpty - with Enumeratum module enabled: - enums based on EnumEntry/Enum - enums based on ValueEnumEntry/ValueEnum - Misc: - java.util.UUID - java.net.URL - java.net.URI - Collections: - String Map (eg. Map[String, T]) - Int Map (eg. Map[Int, T]) - Iterable[T] - Sealed Trait hierarchy of case objects (Enums) - Case Classes: - default value - Sealed Trait hierarchy of case classes - Value Classes ``` -------------------------------- ### Generate Distinct Schemas with Scala Value Classes Source: https://github.com/andyglow/scala-jsonschema/blob/master/README.md Explains how to use Scala value classes (`AnyVal`) to create distinct types for fields that might otherwise share the same base type (e.g., String). This approach allows for separate schema definitions and avoids conflicts when different validation rules or representations are needed for conceptually distinct fields. ```scala case class UserId(value: String) extends AnyVal case class User(id: UserId, name: String) ``` ```scala import json._ implicit val userIdSchema: json.Schema[UserId] = Json.schema[UserId].toDefinition("userId") implicit val userSchema: json.Schema[User] = Json.schema[User] println(JsonFormatter.format(AsValue.schema(someArrSchema))) ``` ```json { "$schema": "http://json-schema.org/draft-04/schema#", "type": "object", "additionalProperties": false, "properties": { "id": { "$ref": "#/definitions/userId" }, "name": { "type": "string" }, "required": [ "id", "name" ], "definitions": { "userId": { "type": "string" } } } } ``` -------------------------------- ### Scala-JsonSchema Field Annotations API Source: https://github.com/andyglow/scala-jsonschema/blob/master/README.md API documentation for specific annotations provided by Scala-JsonSchema that can be applied to fields. These annotations allow direct modification of JSON schema properties like `readOnly` and `writeOnly`. ```APIDOC @readOnly: Scope: Field Description: Adds "readOnly": true to property definition @writeOnly: Scope: Field Description: Adds "writeOnly": true to property definition ``` -------------------------------- ### Customize JSON Schema Definition Names Source: https://github.com/andyglow/scala-jsonschema/blob/master/README.md Illustrates how to override the default type-based definition name in the generated JSON schema. By using the `.toDefinition()` method, developers can specify a custom name for a schema definition, which is useful for cleaner or more domain-specific schema references. ```scala import json._ implicit val someStrSchema: json.Schema[String] = Json.schema[String].toDefinition("my-lovely-string") implicit val someArrSchema: json.Schema[Array[String]] = Json.schema[Array[String]] println(JsonFormatter.format(AsValue.schema(someArrSchema, json.schema.Version.Draft04()))) ``` ```json { "$schema": "http://json-schema.org/draft-04/schema#", "type": "array", "items": { "$ref": "#/definitions/my-lovely-string" }, "definitions": { "my-lovely-string": { "type": "string" } } } ``` -------------------------------- ### Default JSON Schema for Scala Enums Source: https://github.com/andyglow/scala-jsonschema/blob/master/README.md Illustrates the default JSON Schema output for a simple Scala `sealed trait` enum without any special flags. The enum values are represented as a string `enum` array. ```scala sealed trait Gender object Gender { case object Male extends Gender case object Female extends Gender } ``` ```json { "type": "string", "enum": [ "Male", "Female" ] } ``` -------------------------------- ### JSON Schema for Enums with OneOf Representation Source: https://github.com/andyglow/scala-jsonschema/blob/master/README.md Demonstrates the JSON Schema output when the `EnumsAsOneOf` flag is enabled. The enum values are represented as a `oneOf` array, where each element is a `const` object, allowing for more flexible schema definitions. ```scala sealed trait Gender object Gender { case object Male extends Gender case object Female extends Gender } ``` ```json { "oneOf": [ { "const": "Male" }, { "const": "Female" } ] } ``` -------------------------------- ### JSON Schema for Enums with Title and Description Metadata Source: https://github.com/andyglow/scala-jsonschema/blob/master/README.md Shows how to add `title` and `description` to individual enum values in the generated JSON Schema. This is achieved by using `@title` annotations and Scaladoc comments on the Scala `sealed trait` enum, which is then reflected in the `oneOf` representation when the `EnumsAsOneOf` flag is enabled. ```scala sealed trait Gender object Gender { @title("The Male") case object Male extends Gender /** The Female */ case object Female extends Gender } ``` ```json { "oneOf": [ { "title": "The Male", "const": "Male" }, { "description": "The Female", "const": "Female" } ] } ``` -------------------------------- ### Configure JSON Schema to Represent Enums as OneOf Source: https://github.com/andyglow/scala-jsonschema/blob/master/README.md Shows how to enable the `EnumsAsOneOf` flag by providing an implicit value. This configuration changes the JSON Schema representation of Scala enums from a simple `enum` array to a `oneOf` array of `const` objects, allowing for richer metadata like titles and descriptions per enum value. ```scala implicit val jsonSchemaFlags: Flag with Flag.EnumsAsOneOf = null ``` -------------------------------- ### Generate In-Lined JSON Schema in Scala Source: https://github.com/andyglow/scala-jsonschema/blob/master/README.md This snippet demonstrates how to generate a JSON schema in 'in-lined' mode using the `scala-jsonschema` library. In this mode, all type definitions are embedded directly within the main schema, resulting in a single, self-contained JSON object without a separate `definitions` block. It's suitable for simpler schemas where reusability of sub-types is not a primary concern. ```scala import json._ val personSchema: json.Schema[Person] = Json.schema[Person] ``` ```json { "$schema": "http://json-schema.org/draft-04/schema#", "type": "object", "additionalProperties": false, "properties": { "middleName": { "type": "string" }, "cars": { "type": "array", "items": { "type": "object", "additionalProperties": false, "properties": { "name": { "type": "string" }, "manufacturer": { "type": "object", "additionalProperties": false, "properties": { "name": { "type": "string" } }, "required": [ "name" ] } }, "required": [ "name", "manufacturer" ] } }, "company": { "type": "object", "additionalProperties": false, "properties": { "name": { "type": "string" } }, "required": [ "name" ] }, "lastName": { "type": "string" }, "firstName": { "type": "string" }, "birthDay": { "type": "string", "format": "date-time" }, "gender": { "type": "string", "enum": [ "Male", "Female" ] } }, "required": [ "company", "lastName", "birthDay", "gender", "firstName", "cars" ] } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.