Skip to content

Commit

Permalink
Improve code layout
Browse files Browse the repository at this point in the history
  • Loading branch information
propensive committed Feb 28, 2024
1 parent d287327 commit e87d079
Showing 1 changed file with 27 additions and 26 deletions.
53 changes: 27 additions & 26 deletions src/core/schema.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,8 @@ object JsonValidationError:

object Reason:
given Communicable[Reason] =
case JsonType(expected, found) =>
msg"expected JSON type $expected, but found $found"

case MissingValue =>
msg"the value was missing"
case JsonType(expected, found) => msg"expected JSON type $expected, but found $found"
case MissingValue => msg"the value was missing"

case IntOutOfRange(value, minimum, maximum) =>
if minimum.absent then msg"the value was greater than the maximum, ${maximum.or(0)}"
Expand Down Expand Up @@ -137,17 +134,16 @@ object JsonRecord:
given array: RecordAccessor[JsonRecord, Optional[Json], "array", List] =
_.vouch(using Unsafe).as[List[Json]].map(_)

given obj: RecordAccessor[JsonRecord, Optional[Json], "object", [T] =>> T] = (value, make) =>
make(value.vouch(using Unsafe))
given obj: RecordAccessor[JsonRecord, Optional[Json], "object", [Type] =>> Type] =
(value, make) => make(value.vouch(using Unsafe))

given maybeBoolean: ValueAccessor[JsonRecord, Optional[Json], "boolean?", Optional[Boolean]] =
(value, params) => value.let(_.as[Boolean])

given maybeString: ValueAccessor[JsonRecord, Optional[Json], "string?", Optional[Text]] =
(value, params) => value.let(_.as[Text])

given pattern
: ValueAccessor[JsonRecord, Optional[Json], "pattern", Text] with
given pattern: ValueAccessor[JsonRecord, Optional[Json], "pattern", Text] with
def transform(value: Optional[Json], params: List[String]): Text =
value.let: value =>
(params: @unchecked) match
Expand All @@ -158,9 +154,7 @@ object JsonRecord:
.or(abort(JsonValidationError(MissingValue)))

given maybePattern: ValueAccessor[JsonRecord, Optional[Json], "pattern?", Optional[Text]] with
def transform
(value: Optional[Json], params: List[String] = Nil)
: Optional[Text] =
def transform(value: Optional[Json], params: List[String] = Nil): Optional[Text] =
value.let: value =>
(params: @unchecked) match
case pattern :: Nil =>
Expand All @@ -171,13 +165,12 @@ object JsonRecord:
given maybeInteger: ValueAccessor[JsonRecord, Optional[Json], "integer?", Optional[Int]] =
(value, params) => value.let(_.as[Int])

given boundedInteger
: ValueAccessor[JsonRecord, Optional[Json], "integer!", Int raises IntRangeError] =
given boundedInteger: ValueAccessor[JsonRecord, Optional[Json], "integer!", Int raises IntRangeError] =
new ValueAccessor[JsonRecord, Optional[Json], "integer!", Int raises IntRangeError]:
def transform(json: Optional[Json], params: List[String] = Nil): Int raises IntRangeError =
val int = json.vouch(using Unsafe).as[Int]

(params.map(Text(_)): @unchecked) match
(params.map(_.tt): @unchecked) match
case As[Int](min) :: As[Int](max) :: Nil =>
if int < min || int > max then abort(IntRangeError(int, min, max)) else int

Expand All @@ -200,18 +193,29 @@ class JsonRecord(data: Optional[Json], access: String => Optional[Json] => Any)
extends Record[Optional[Json]](data, access)

case class JsonSchemaDoc
(`$schema`: Text, `$id`: Text, title: Text, `type`: Text,
properties: Map[String, JsonSchema.Property], required: Optional[Set[String]]):
(`$schema`: Text,
`$id`: Text,
title: Text,
`type`: Text,
properties: Map[String, JsonSchema.Property],
required: Optional[Set[String]]):

lazy val requiredFields: Set[String] = required.or(Set())

def fields: Map[String, RecordField] =
properties.map { (key, value) => key -> value.field(requiredFields.contains(key)) }

object JsonSchema:
case class Property
(`type`: String, properties: Optional[Map[String, Json]], items: Optional[Map[String, Json]],
required: Optional[Set[String]], minimum: Optional[Int], maximum: Optional[Int],
format: Optional[String], pattern: Optional[String]):
(`type`: String,
properties: Optional[Map[String, Json]],
items: Optional[Map[String, Json]],
required: Optional[Set[String]],
minimum: Optional[Int],
maximum: Optional[Int],
format: Optional[String],
pattern: Optional[String]):

def requiredFields: Set[String] = required.or(Set())

def arrayFields =
Expand All @@ -225,11 +229,8 @@ object JsonSchema:
).or(throw Panic(msg"Some properties were missing"))

def field(required: Boolean): RecordField = `type` match
case "array" =>
RecordField.Record(if required then "array" else "array?", arrayFields)

case "object" =>
RecordField.Record(if required then "object" else "object?", objectFields)
case "array" => RecordField.Record(if required then "array" else "array?", arrayFields)
case "object" => RecordField.Record(if required then "object" else "object?", objectFields)

case "string" =>
val suffix = if required then "" else "?"
Expand All @@ -250,4 +251,4 @@ abstract class JsonSchema(val doc: JsonSchemaDoc) extends Schema[Optional[Json],
def make(data: Optional[Json], access: String => Optional[Json] => Any): JsonRecord =
JsonRecord(data, access)

def fields: Map[String, RecordField] = unsafely(doc.fields)
def fields: Map[String, RecordField] = unsafely(doc.fields)

0 comments on commit e87d079

Please sign in to comment.