keyword = $keyword; $this->limit = $limit; } /** * Checks the date against its limit without changing either value. * * @since 11.2.0 * * @param ValidationContext $context The current document and value. * @param Schema $schema The schema being evaluated. * @return ValidationError|null The comparison error, if any. */ public function validate( ValidationContext $context, Schema $schema ): ?ValidationError { $limit = $this->limit instanceof JsonPointer ? $this->limit->data( $context->rootData(), $context->currentDataPath(), $this ) : $this->limit; // AJV skips an unresolved $data reference, but rejects one with the wrong type. if ( $this === $limit ) { return null; } if ( ! is_string( $limit ) ) { return $this->error( $schema, $context, $this->keyword, 'The date limit must be a string.' ); } // ajv-formats does not compare dates when either string is empty. if ( '' === $limit || '' === $context->currentData() ) { return null; } $comparison = strcmp( $context->currentData(), $limit ); switch ( $this->keyword ) { case 'formatMinimum': $valid = $comparison >= 0; break; case 'formatMaximum': $valid = $comparison <= 0; break; case 'formatExclusiveMinimum': $valid = $comparison > 0; break; case 'formatExclusiveMaximum': $valid = $comparison < 0; break; default: return $this->error( $schema, $context, $this->keyword, 'Unsupported date comparison keyword.' ); } return $valid ? null : $this->error( $schema, $context, $this->keyword, 'Date does not satisfy {keyword}: {limit}.', array( 'keyword' => $this->keyword, 'limit' => $limit, ) ); } }