All Downloads are FREE. Search and download functionalities are using the official Maven repository.

smithy.api.TraitValidators.scala Maven / Gradle / Ivy

There is a newer version: 0.19.0-41-91762fb
Show newest version
package smithy.api

import smithy4s.Hints
import smithy4s.Newtype
import smithy4s.Schema
import smithy4s.ShapeId
import smithy4s.schema.Schema.bijection
import smithy4s.schema.Schema.map
import smithy4s.schema.Schema.recursive
import smithy4s.schema.Schema.string

/** A meta-trait used to apply validation to a specific shape in the model that a trait is applied to.
  * 
  * `traitValidators` is a map of validation event IDs to validators to apply to a shape.
  * Selectors are used to identify shapes that are incompatible with a constrained trait.
  * The shape with the targeted trait applied MUST be a valid entry point for the selector.
  * 
  * The following example defines a protocol that does not support document types. Each matching member found in the
  * closure of an attached shape emits a validation event:
  * 
  * ```
  * {@literal @}trait(selector: "service")
  * {@literal @}traitValidators(
  *     "myCustomProtocol.NoDocuments": {
  *         selector: "~> member :test(> document)"
  *         message: "This protocol does not support document types"
  *     }
  * )
  * {@literal @}protocolDefinition
  * structure myCustomProtocol {}
  * ```
  * @param key
  *   The validation event ID to emit when the constraint finds an incompatible shape.
  * @param value
  *   The validator to apply.
  */
object TraitValidators extends Newtype[Map[String, TraitValidator]] {
  val id: ShapeId = ShapeId("smithy.api", "traitValidators")
  val hints: Hints = Hints(
    smithy.api.Documentation("A meta-trait used to apply validation to a specific shape in the model that a trait is applied to.\n\n`traitValidators` is a map of validation event IDs to validators to apply to a shape.\nSelectors are used to identify shapes that are incompatible with a constrained trait.\nThe shape with the targeted trait applied MUST be a valid entry point for the selector.\n\nThe following example defines a protocol that does not support document types. Each matching member found in the\nclosure of an attached shape emits a validation event:\n\n```\n@trait(selector: \"service\")\n@traitValidators(\n    \"myCustomProtocol.NoDocuments\": {\n        selector: \"~> member :test(> document)\"\n        message: \"This protocol does not support document types\"\n    }\n)\n@protocolDefinition\nstructure myCustomProtocol {}\n```"),
    smithy.api.Trait(selector = Some("[trait|trait]"), structurallyExclusive = None, conflicts = None, breakingChanges = None),
  ).lazily
  val underlyingSchema: Schema[Map[String, TraitValidator]] = map(string.addMemberHints(smithy.api.Documentation("The validation event ID to emit when the constraint finds an incompatible shape.")).validated(smithy.api.Length(min = Some(1L), max = None)), TraitValidator.schema.addMemberHints(smithy.api.Documentation("The validator to apply."))).withId(id).addHints(hints)
  implicit val schema: Schema[TraitValidators] = recursive(bijection(underlyingSchema, asBijection))
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy