Skip to content

Commit

Permalink
update docs, define CheckInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
eddmash committed Nov 25, 2017
1 parent ee8c4ce commit ad6c2ae
Show file tree
Hide file tree
Showing 46 changed files with 681 additions and 320 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ Using this library boils down to this steps
- Create a validator object

```
// validator takes Context(Activity) object as argument
Validator validator = new Validator(this);
Validator validator = new Validator();
```

- Add validation checks to the validator
Expand Down
64 changes: 64 additions & 0 deletions docs/source/checks.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,69 @@
Checks
######

.. contents::
:depth: 2

Introduction
************

A Check is a class that implements the :java:ref:`CheckInterface <CheckInterface>`. The interface
defines a few methods.

- :java:ref:`run() <CheckInterface.run()>` This is where all the logic of validation is placed.
This method should return ``true`` if validation succeeds or ``false`` on failure.
- :java:ref:`getErrorMsg() <CheckInterface.getErrorMsg()>` this should the error message to be used
incase validation fails.
- :java:ref:`setError() <CheckInterface.setError()>` This method is invoked to with the error
message above as an argument. This method should be used to set the error message on the view.
- :java:ref:`clearError() <CheckInterface.clearError()>` This method is invoked just be validation
is run. it should be used to clear any previous error displayed on the validator.

Creating custom validations
***************************

You can create a custom checks by implementing :java:ref:`CheckInterface <CheckInterface>`,
or by extending one of the :ref:`built-in checks<build_in_checks>`.

As an example we create a simple check that checks if a string contains only
alphanumeric characters.

.. code-block:: java
class ContainsAlphaNumericCheck implements CheckInterface {
private EditText editText;
public ContainsAlphaNumericCheck(EditText editText) {
this.editText = editText;
}
@Override
public boolean run() {
String value = editText.getText().toString();
return Pattern.compile("^[a-zA-Z0-9]+$").matcher(value).matches();
}
@Override
public String getErrorMsg() {
return "String contains an illegal character: it can only contain letters or numbers.";
}
@Override
public void setError(String error) {
editText.setError(error);
}
@Override
public void clearError() {
editText.setError(null);
}
}
.. _build_in_checks:

Built-in Checks
****************

.. toctree::
:maxdepth: 2
:titlesonly:
Expand Down
11 changes: 8 additions & 3 deletions docs/source/com/eddmash/validation/ValidationListener.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,24 @@ ValidationListener
.. java:package:: com.eddmash.validation
:noindex:

.. java:type:: interface ValidationListener
.. java:type:: public interface ValidationListener
Interface definition for callbacks to be invoked when the validation state has changed.

Methods
-------
onValidationFailed
^^^^^^^^^^^^^^^^^^

.. java:method:: void onValidationFailed()
.. java:method:: void onValidationFailed(ValidatorInterface validatorInterface)
:outertype: ValidationListener

Invoked when validation failed

.. java:method:: void onValidationSuccess()
onValidationSuccess
^^^^^^^^^^^^^^^^^^^

.. java:method:: void onValidationSuccess(ValidatorInterface validatorInterface)
:outertype: ValidationListener

Invoked when the validation passed successfully.
Expand Down
48 changes: 42 additions & 6 deletions docs/source/com/eddmash/validation/Validator.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
.. java:import:: android.util Log
.. java:import:: com.eddmash.validation.checks ValidationCheck
.. java:import:: com.eddmash.validation.checks CheckInterface
.. java:import:: com.eddmash.validation.checks CheckSingle
.. java:import:: java.util ArrayList
Expand All @@ -26,45 +28,79 @@ Validator
Constructors
------------
Validator
^^^^^^^^^

.. java:constructor:: public Validator(Activity context)
.. java:constructor:: public Validator()
:outertype: Validator

.. java:constructor:: public Validator(String tag, Activity context)
Validator
^^^^^^^^^

.. java:constructor:: public Validator(String tag)
:outertype: Validator

Methods
-------
addCheck
^^^^^^^^

.. java:method:: @Override public void addCheck(ValidationCheck validationCheck)
.. java:method:: @Override public void addCheck(CheckInterface checkInterface)
:outertype: Validator

addValidator
^^^^^^^^^^^^

.. java:method:: @Override public void addValidator(ValidatorInterface validator)
:outertype: Validator

clearErrors
^^^^^^^^^^^

.. java:method:: @Override public void clearErrors()
:outertype: Validator

.. java:method:: @Override public void disableCheck(ValidationCheck validationCheck)
disableCheck
^^^^^^^^^^^^

.. java:method:: @Override public void disableCheck(CheckInterface checkInterface)
:outertype: Validator

\ :java:ref:`see <ValidatorInterface.addCheck(ValidationCheck)>`\
\ :java:ref:`see <ValidatorInterface.addCheck(CheckInterfacecheckInterface)>`\

disableValidator
^^^^^^^^^^^^^^^^

.. java:method:: @Override public void disableValidator(ValidatorInterface validatorInterface)
:outertype: Validator

getErrors
^^^^^^^^^

.. java:method:: @Override public Map<String, List> getErrors()
:outertype: Validator

getErrorsByTag
^^^^^^^^^^^^^^

.. java:method:: @Override public List getErrorsByTag(String tag)
:outertype: Validator

toString
^^^^^^^^

.. java:method:: @Override public String toString()
:outertype: Validator

validate
^^^^^^^^

.. java:method:: @Override public boolean validate()
:outertype: Validator

validate
^^^^^^^^

.. java:method:: @Override public void validate(ValidationListener validationListener)
:outertype: Validator

38 changes: 33 additions & 5 deletions docs/source/com/eddmash/validation/ValidatorInterface.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.. java:import:: com.eddmash.validation.checks ValidationCheck
.. java:import:: com.eddmash.validation.checks CheckInterface
.. java:import:: com.eddmash.validation.checks CheckSingle
.. java:import:: java.util List
Expand All @@ -14,13 +16,18 @@ ValidatorInterface
Methods
-------
addCheck
^^^^^^^^

.. java:method:: void addCheck(ValidationCheck validationCheck)
.. java:method:: void addCheck(CheckInterface checkInterface)
:outertype: ValidatorInterface

Adds validation checks to be enforced by a validator

:param validationCheck:
:param checkInterface:

addValidator
^^^^^^^^^^^^

.. java:method:: void addValidator(ValidatorInterface validatorInterface)
:outertype: ValidatorInterface
Expand All @@ -29,17 +36,26 @@ Methods

:param validatorInterface: the validatorInterface object

clearErrors
^^^^^^^^^^^

.. java:method:: void clearErrors()
:outertype: ValidatorInterface

Clear all the errors from the validator. maybe use when you have already run the validation onces and want to run the validation again using the same ValidatorInterface instance

.. java:method:: void disableCheck(ValidationCheck validationCheck)
disableCheck
^^^^^^^^^^^^

.. java:method:: void disableCheck(CheckInterface checkInterface)
:outertype: ValidatorInterface

disable validation check

:param validationCheck: the validation check to disable.
:param checkInterface: the validation check to disable.

disableValidator
^^^^^^^^^^^^^^^^

.. java:method:: void disableValidator(ValidatorInterface validatorInterface)
:outertype: ValidatorInterface
Expand All @@ -48,6 +64,9 @@ Methods

:param validatorInterface: validatorInterface object

getErrors
^^^^^^^^^

.. java:method:: Map<String, List> getErrors()
:outertype: ValidatorInterface

Expand All @@ -57,20 +76,29 @@ Methods

:return: Map

getErrorsByTag
^^^^^^^^^^^^^^

.. java:method:: List getErrorsByTag(String tag)
:outertype: ValidatorInterface

Gets a list of errors for a specific tag.

:param tag:

validate
^^^^^^^^

.. java:method:: boolean validate()
:outertype: ValidatorInterface

Does the actual validation.

:return: boolean true of valid

validate
^^^^^^^^

.. java:method:: void validate(ValidationListener validationListener)
:outertype: ValidatorInterface

Expand Down
10 changes: 6 additions & 4 deletions docs/source/com/eddmash/validation/checks/AllCheck.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
.. java:import:: android.widget TextView
AllCheck
========

Expand All @@ -8,17 +6,21 @@ AllCheck

.. java:type:: public class AllCheck extends CheckCompound
Ensures all are validation checks are valid. NOTE:: if the no checks are provided i.e. checkList is empty, validation will always be valid.
Ensures all are validation checks are valid. \ **NOTE::**\ If the no checks are provided i.e. checkList is empty, validation will always pass for this check.

Constructors
------------
AllCheck
^^^^^^^^

.. java:constructor:: public AllCheck(String errorMessage)
:outertype: AllCheck

Methods
-------
run
^^^

.. java:method:: @Override protected boolean validate()
.. java:method:: @Override public boolean run()
:outertype: AllCheck

10 changes: 6 additions & 4 deletions docs/source/com/eddmash/validation/checks/AnyCheck.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
.. java:import:: android.widget TextView
AnyCheck
========

Expand All @@ -8,17 +6,21 @@ AnyCheck

.. java:type:: public class AnyCheck extends CheckCompound
Checks if at least one of the checks passed validation. NOTE:: if the no checks are provided i.e. checkList is empty, validation will always be failed.
Checks if at least one of the checks passed validation. \ **NOTE::**\ If the no checks are provided i.e. checkList is empty, validation will always fail.

Constructors
------------
AnyCheck
^^^^^^^^

.. java:constructor:: public AnyCheck(String errorMessage)
:outertype: AnyCheck

Methods
-------
run
^^^

.. java:method:: @Override protected boolean validate()
.. java:method:: @Override public boolean run()
:outertype: AnyCheck

0 comments on commit ad6c2ae

Please sign in to comment.