{"id":15575314,"url":"https://github.com/vickumar1981/svalidate","last_synced_at":"2025-06-14T01:32:27.841Z","repository":{"id":57723874,"uuid":"165548444","full_name":"vickumar1981/svalidate","owner":"vickumar1981","description":"Simple and easy validations for Scala and Java","archived":false,"fork":false,"pushed_at":"2022-01-07T22:26:38.000Z","size":1125,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-19T07:42:49.918Z","etag":null,"topics":["case-classes","dsl","hacktoberfest","hacktoberfest-accepted","hacktoberfest2021","java-validation","java-validation-framework","java-validator","scala-library","scala-validation","validation"],"latest_commit_sha":null,"homepage":"","language":"Scala","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vickumar1981.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-01-13T20:00:55.000Z","updated_at":"2022-05-16T18:19:07.000Z","dependencies_parsed_at":"2022-08-28T14:02:02.335Z","dependency_job_id":null,"html_url":"https://github.com/vickumar1981/svalidate","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vickumar1981%2Fsvalidate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vickumar1981%2Fsvalidate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vickumar1981%2Fsvalidate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vickumar1981%2Fsvalidate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vickumar1981","download_url":"https://codeload.github.com/vickumar1981/svalidate/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243236124,"owners_count":20258792,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["case-classes","dsl","hacktoberfest","hacktoberfest-accepted","hacktoberfest2021","java-validation","java-validation-framework","java-validator","scala-library","scala-validation","validation"],"created_at":"2024-10-02T18:28:52.014Z","updated_at":"2025-03-12T14:40:17.739Z","avatar_url":"https://github.com/vickumar1981.png","language":"Scala","readme":"![Logo](logo.png)\n\n## sValidate\n\n[![Build Status](https://api.travis-ci.com/vickumar1981/svalidate.svg?branch=master)](https://app.travis-ci.com/github/vickumar1981/svalidate/builds) [![Coverage Status](https://coveralls.io/repos/github/vickumar1981/svalidate/badge.svg?branch=master)](https://coveralls.io/github/vickumar1981/svalidate?branch=master) [![Read the Docs](https://img.shields.io/readthedocs/pip.svg)](https://vickumar1981.github.io/svalidate/api/com/github/vickumar1981/svalidate/index.html) [![Maven metadata URI](https://img.shields.io/maven-central/v/com.github.vickumar1981/svalidate_2.12)](https://mvnrepository.com/artifact/com.github.vickumar1981/svalidate) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](LICENSE.md)\n\nA simple and easy validation syntax for Scala and Java classes\n\nFor more detailed information, please refer to the [API Documentation](https://vickumar1981.github.io/svalidate/api/com/github/vickumar1981/svalidate/index.html \"API Documentation\").\n\nRequires: Scala 2.12, 2.11\n\n---\n### Contents\n\n1.  [Add it to your project](https://github.com/vickumar1981/svalidate#1-add-it-to-your-project-)\n2.  [Basic Usage](https://github.com/vickumar1981/svalidate#2-basic-usage)\n3.  [Nested Classes](https://github.com/vickumar1981/svalidate#3-nested-classes)\n4.  [Validating With](https://github.com/vickumar1981/svalidate#4-validating-with)\n5.  [Reporting an Issue](https://github.com/vickumar1981/svalidate#5-reporting-an-issue)\n6.  [Contributing](https://github.com/vickumar1981/svalidate#6-contributing)\n7.  [License](https://github.com/vickumar1981/svalidate#7-license)\n\n---\n### 1. Add it to your project ...\n\n__Using sbt:__\n\nIn `build.sbt`:\n```scala\nlibraryDependencies += \"com.github.vickumar1981\" %% \"svalidate\" % \"1.0.1\"\n```\n\n__Using gradle:__\n\nIn `build.gradle`:\n```groovy\ndependencies {\n    compile 'com.github.vickumar1981:svalidate_2.12:1.0.1'\n}\n```\n\n__Using Maven:__\n\nIn `pom.xml`:\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003ecom.github.vickumar1981\u003c/groupId\u003e\n    \u003cartifactId\u003esvalidate_2.12\u003c/artifactId\u003e\n    \u003cversion\u003e1.0.1\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\nNote: For Java 7 or Scala 2.11, please use the `svalidate_2.11` artifact as a dependency instead.\n\n---\n### 2. Basic Usage\n\nLet's create a simple class called `Address` that looks like:\n\n```scala\npackage test.example\n\ncase class Address(street: String,\n                   city: String,\n                   state: String,\n                   zipCode: String)\n```\n\nThe rules for validating an `Address` are:\n -  street address is required\n -  city is required\n -  state abbr. must be exactly two capital letters\n -  zip code must be exactly 5 digits\n\n\nUsing `sValidate`'s validation syntax, we can add a new validator of the type `Validatable\u003cAddress\u003e`\n\n```scala\npackage test.example\n\nimport com.github.vickumar1981.svalidate.{Validatable, Validation}\n\nobject ModelValidations {\n  implicit object AddressValidator extends Validatable[Address] {\n      override def validate(value: Address): Validation = {\n        (value.street.nonEmpty orElse \"Street addr. is required\") ++\n          (value.city.nonEmpty orElse \"City is required\") ++\n          (value.zipCode.matches(\"\\\\d{5}\") orElse \"Zip code must be 5 digits\") ++\n          (value.state.matches(\"[A-Z]{2}\") orElse \"State abbr must be 2 letters\")\n      }\n    }\n}\n```\n\nTo extend validation to the `Address` class, we import our validator and the validation syntax.\n\n```scala\nimport test.example.Address\n\nobject TestValidation {\n  import test.example.ModelValidations._\n  import com.github.vickumar1981.svalidate.ValidationSyntax._\n  \n  def main(args: Array[String]): Unit = {\n    val addr = Address(\"\", \"\", \"\", \"\")\n    val errors = addr.validate().errors\n    println(errors)\n    // ArrayBuffer(Street addr. is required, City is required, Zip code must be 5 digits, State abbr must be 2 letters)\n  }\n}\n```\n\n[See Scala Address validation example](https://github.com/vickumar1981/svalidate/blob/master/src/main/scala/com/github/vickumar1981/svalidate/example/model/package.scala#L28)\n \n[See Java Address validation example](https://github.com/vickumar1981/svalidate/blob/master/src/main/java/com/github/vickumar1981/svalidate/util/example/model/Address.java#L7)\n\n---\n### 3. Nested Classes\n\nLet's say we have a `Person` class which contains an `Address` instance, \nand whose validation depends upon the validation of the `address` member instance.\n\nAdditionally, a `Person` also has a `hasContact: Boolean` indicator\n\nThe class might look like:\n\n```scala\ncase class Person(firstName: String,\n                  lastName: String,\n                  hasContactInfo: Boolean,\n                  address: Option[Address] = None,\n                  phone: Option[String] = None)\n\n```\n\nThe rules for validating a `Person` are:\n -  first name is required\n -  last name is required\n -  phone number and address are both optional, and their\n    validation depends upon the `hasContactInfo` indicator\n -  a phone number must be exactly 10 numbers\n -  if the `hasContactInfo` flag is true, then both `phone` and `address`\n    should be validated\n -  if the `hasContactInfo` flag is false, then both `phone` and `address`\n    should be empty\n\nAn example validator for `Person` might look like: \n\n```scala\npackage text.example\n\nimport com.github.vickumar1981.svalidate.{Validatable, Validation}\n\nobject ModelValidations {\n  implicit object PersonValidator extends Validatable[Person] {\n      def validateContactInfo(value: Person): Validation = {\n        (value.address errorIfEmpty \"Address is required\") ++\n          (value.phone errorIfEmpty \"Phone # is required\") ++\n          value.address.maybeValidate() ++\n          value.phone.maybeValidate(_.matches(\"\\\\d{10}\") orElse \"Phone # must be 10 digits\")\n      }\n\n      override def validate(value: Person): Validation = {\n        (value.firstName.nonEmpty orElse \"First name is required\") ++\n          (value.lastName.nonEmpty orElse \"Last name is required\") ++\n          (value.hasContactInfo andThen validateContactInfo(value)) ++\n          value.hasContactInfo.orElse {\n            (value.address errorIfDefined \"Address must be empty\") ++\n              (value.phone errorIfDefined \"Phone # must be empty\")\n          }\n      }\n  }\n}\n```\n\n[See Scala Person validation example](https://github.com/vickumar1981/svalidate/blob/master/src/main/scala/com/github/vickumar1981/svalidate/example/model/package.scala#L37)\n \n[See Java Person validation example](https://github.com/vickumar1981/svalidate/blob/master/src/main/java/com/github/vickumar1981/svalidate/util/example/model/Person.java#L12)\n\n---\n### 4. Validating With\n\nSometimes, validation depends on an external value.  This is where we can use the `.validateWith[T](t: T)` syntax.\n\nLet's say we have a `Contacts` class which contains an optional list of Facebook and Twitter emails.\n\nEach user in our system also has a `ContactSettings` object, that determines the validation of the user's `Contacts`\n\nThe two classes might look like:\n\n```scala\ncase class Contacts(facebook: Option[List[String]] = None, twitter: Option[List[String]] = None)\ncase class ContactSettings(hasFacebookContacts: Option[Boolean] = Some(true),\n                           hasTwitterContacts: Option[Boolean] = Some(true))\n```\n\nThe rules for validating a user's `Contacts` are:\n -  If the `hasFacebookContacts` or `hasTwitterContacts` indicators are set to `true`,\n    then the respective `facebook` or `twitter` list of emails for a user must be supplied\n -  If the `hasFacebookContacts` or `hasTwitterContacts` indicators are set to `false`,\n    then the respective `facebook` or `twitter` list of emails for a user must be empty\n -  If the `hasFacebookContacts` or `hasTwitterContacts` indicators are empty,\n    then the respective `facebook` or `twitter` list of emails can be empty or supplied\n\nWe will use a `ValidatableWith[Contacts, ContactSettings]` validator. \n\nAn example implementation might look like:\n\n```scala\npackage text.example\n\nimport com.github.vickumar1981.svalidate.{ValidatableWith, Validation}\n\nobject ModelValidations {\n  implicit object ContactInfoValidator extends ValidatableWith[Contacts, ContactSettings] {\n      override def validateWith(value: Contacts, contactSettings: ContactSettings): Validation = {\n        contactSettings.hasFacebookContacts.maybeValidate {\n          contacts =\u003e\n            (contacts andThen { value.facebook errorIfEmpty \"Facebook contacts are required\" }) ++\n              (contacts orElse { value.facebook errorIfDefined \"Facebook contacts must be empty\"})\n        } ++\n          contactSettings.hasTwitterContacts.maybeValidate {\n            contacts =\u003e\n              (contacts andThen { value.twitter errorIfEmpty \"Twitter contacts are required\" }) ++\n                (contacts orElse { value.twitter errorIfDefined \"Twitter contacts must be empty\" })\n          }\n      }\n  }\n}\n```\n\nAn example of using `.validateWith`:\n\n```scala\nimport test.example.{ContactSettings, ContactInfo}\n\nobject TestValidation {\n  import test.example.ModelValidations._\n  import com.github.vickumar1981.svalidate.ValidationSyntax._\n  \n  def main(args: Array[String]): Unit = {\n    val contacts = Contacts(None, None)\n    val contactSettings = ContactSettings(None, None)\n    val result = contact.validateWith(contactSettings)\n    println(result.isSuccess)\n    // true\n  }\n}\n```\n\n[See Scala Contacts validation example](https://github.com/vickumar1981/svalidate/blob/master/src/main/scala/com/github/vickumar1981/svalidate/example/model/package.scala#L60)\n\nNote:  There is currently no `.validateWith` syntax for Java\n\n---\n### 5. Reporting an Issue\n\nPlease report any issues or bugs to the [Github issues page](https://github.com/vickumar1981/svalidate/issues).\n\n---\n### 6. Contributing\n\nPlease view the [contributing guidelines](CONTRIBUTING.md) \n\n---\n### 7. License\n\nThis project is licensed under the [Apache 2 License](LICENSE.md).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvickumar1981%2Fsvalidate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvickumar1981%2Fsvalidate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvickumar1981%2Fsvalidate/lists"}