3 namespace OpenEMR\Validators
;
5 use OpenEMR\Common\Uuid\UuidRegistry
;
6 use OpenEMR\Validators\ProcessingResult
;
7 use Particle\Validator\Validator
;
8 use Ramsey\Uuid\Exception\InvalidUuidStringException
;
11 * Base class for OpenEMR object validation.
12 * Validation processes are implemented using Particle (https://github.com/particle-php/Validator)
14 * @link http://www.open-emr.org
15 * @author Dixon Whitmire <dixonwh@gmail.com>
16 * @copyright Copyright (c) 2020 Jerry Padgett <sjpadgett@gmail.com>
17 * @copyright Copyright (c) 2020 Dixon Whitmire <dixonwh@gmail.com>
18 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
20 abstract class BaseValidator
22 // supported validation contexts for database operations
23 public const DATABASE_INSERT_CONTEXT
= "db-insert";
24 public const DATABASE_UPDATE_CONTEXT
= "db-update";
28 protected $supportedContexts;
31 * Configures the validator instance with validation requirements and rules.
32 * This default implementation sets the validator's supported context to include
33 * database inserts and updates.
35 protected function configureValidator()
37 array_push($this->supportedContexts
, self
::DATABASE_INSERT_CONTEXT
, self
::DATABASE_UPDATE_CONTEXT
);
40 public function __construct()
42 $this->validator
= new Validator();
43 $this->supportedContexts
= [];
44 $this->configureValidator();
48 * @return true if the requested context is supported by the validator instance.
50 private function isValidContext($context)
52 return in_array($context, $this->supportedContexts
);
56 * Performs a data validation using the configured rules and requirements.
58 * Validation results are conveyed by an array with the following keys:
59 * - isValid => true|false
60 * - messages => array(validationMessage, validationMessage, etc)
62 * @param $dataFields - The fields to validate.
63 * @param $context - The validation context to utilize. This is simply a "handle" for the rules.
64 * @return $validationResult array
66 public function validate($dataFields, $context)
68 if (!$this->isValidContext($context)) {
69 throw new \
RuntimeException("unsupported context: " . $context);
72 $validationResult = $this->validator
->validate($dataFields, $context);
74 $result = new ProcessingResult();
75 $result->setValidationMessages($validationResult->getMessages());
81 * Validates that a ID exists in the database.
83 * @param $field The identifier field in database
84 * @param $table The table in database
85 * @param $lookupId The identifier to validateId
86 * @param $isUuid true if the lookupId is UUID, otherwise false
87 * @return true if the lookupId is a valid existing id, otherwise Validation Message
89 public static function validateId($field, $table, $lookupId, $isUuid = false)
91 $validationResult = new ProcessingResult();
94 $validationMessages = [
95 $field => ["invalid or nonexisting value" => "value " . $lookupId],
97 $validationResult->setValidationMessages($validationMessages);
99 // Check if $id is not UUID or a Valid Integer
102 $lookupId = UuidRegistry
::uuidToBytes($lookupId);
103 } catch (InvalidUuidStringException
$e) {
104 return $validationResult;
106 } elseif (!is_int(intval($lookupId))) {
107 return $validationResult;
111 "SELECT $field FROM $table WHERE $field = ?",
114 if (!empty($result[$field])) {
117 return $validationResult;
122 * Validates that a Code from Valueset exists in the database.
124 * @param $code The code which needs to be verified
125 * @param $table The table in database
126 * @param $valueset Name of the particular Valueset
129 public function validateCode($code, $table, $valueset)
131 $sql = "SELECT option_id FROM $table WHERE list_id = ? AND option_id = ?";
134 array($valueset, $code)
136 return $result['option_id'] ?
true : false;