Field names
Field name can be set or retrieved using macro:
import pl.muninn.simple.validation._
case class Field(name:String, otherField:String)
val macroSchema:ValidationSchema[Field] = createSchema { context =>
context.field(_.name).notEmpty +
context.field(_.otherField).notEmpty
}
// macroSchema: ValidationSchema[Field] = <function1>
macroSchema.validate(Field("",""))
// res0: ValidationResult = Invalid(
// e = Append(
// leftNE = Singleton(a = EmptyField(field = "name")),
// rightNE = Singleton(a = EmptyField(field = "otherField"))
// )
// )
val customNameSchema:ValidationSchema[Field] = createSchema { context =>
context.field("name")(_.name).notEmpty +
context.field("myName")(_.otherField).notEmpty
}
// customNameSchema: ValidationSchema[Field] = <function1>
customNameSchema.validate(Field("",""))
// res1: ValidationResult = Invalid(
// e = Append(
// leftNE = Singleton(a = EmptyField(field = "name")),
// rightNE = Singleton(a = EmptyField(field = "myName"))
// )
// )
Macro names
Macro design for retrieving value name was done in a way to allow user get complex name in easy way. For example:
import pl.muninn.simple.validation._
case class ComplexOtherField(otherField:String)
case class ComplexField(field:Option[ComplexOtherField])
val schema:ValidationSchema[ComplexField] = createSchema { context =>
// field name will be `field.otherField`
context.field(_.field.map(_.otherField)).definedAnd(notEmptyString)
}
// schema: ValidationSchema[ComplexField] = <function1>
schema.validate(ComplexField(None))
// res2: ValidationResult = Invalid(
// e = Singleton(a = EmptyField(field = "field.otherField"))
// )
schema.validate(ComplexField(Some(ComplexOtherField("value"))))
// res3: ValidationResult = Valid(a = ())