root = $schema; } /** * Load a schema file from the contract folder. * * @param string $path Absolute path to a .json schema. * @return Aha_Is_Woocommerce_Json_Schema|null Null when the file is missing or unreadable. */ public static function from_file( $path ) { if ( ! is_readable( $path ) ) { return null; } $decoded = json_decode( (string) file_get_contents( $path ) ); if ( ! is_object( $decoded ) ) { return null; } return new self( $decoded ); } /** * Validate a value. * * @param mixed $data Value decoded with json_decode( $json ) (objects, not arrays). * @return bool True when the value matches. */ public function is_valid( $data ) { $this->errors = array(); $this->check( $data, $this->root, '' ); return empty( $this->errors ); } /** * Errors from the last is_valid() call. * * @return string[] */ public function get_errors() { return $this->errors; } /** * First error from the last is_valid() call. * * @return string */ public function get_first_error() { return empty( $this->errors ) ? '' : $this->errors[0]; } /** * Check one value against one schema. * * @param mixed $data Value. * @param object $schema Schema node. * @param string $path JSON pointer style path, for the error message. * @return void */ private function check( $data, $schema, $path ) { if ( ! is_object( $schema ) ) { return; } if ( isset( $schema->{'$ref'} ) ) { $resolved = $this->resolve_ref( $schema->{'$ref'} ); if ( null !== $resolved ) { $this->check( $data, $resolved, $path ); } return; } if ( isset( $schema->type ) && ! $this->matches_type( $data, $schema->type ) ) { $expected = is_array( $schema->type ) ? implode( ' or ', $schema->type ) : $schema->type; $this->add_error( $path, 'must be of type ' . $expected ); return; // Other keywords make no sense once the type is wrong. } if ( isset( $schema->const ) && $data !== $schema->const ) { $this->add_error( $path, 'must be equal to ' . aha_is_woocommerce_json_encode_compat( $schema->const ) ); } if ( isset( $schema->enum ) && is_array( $schema->enum ) && ! in_array( $data, $schema->enum, true ) ) { $this->add_error( $path, 'must be one of ' . implode( ', ', array_map( 'strval', $schema->enum ) ) ); } if ( isset( $schema->oneOf ) && is_array( $schema->oneOf ) ) { $this->check_one_of( $data, $schema->oneOf, $path ); } if ( isset( $schema->allOf ) && is_array( $schema->allOf ) ) { foreach ( $schema->allOf as $sub ) { $this->check( $data, $sub, $path ); } } if ( is_string( $data ) ) { $this->check_string( $data, $schema, $path ); } if ( is_int( $data ) || is_float( $data ) ) { $this->check_number( $data, $schema, $path ); } if ( is_array( $data ) ) { $this->check_array( $data, $schema, $path ); } if ( is_object( $data ) ) { $this->check_object( $data, $schema, $path ); } } /** * Apply the string keywords. * * @param string $data Value. * @param object $schema Schema node. * @param string $path Path. * @return void */ private function check_string( $data, $schema, $path ) { if ( isset( $schema->minLength ) && strlen( $data ) < (int) $schema->minLength ) { $this->add_error( $path, 'must be at least ' . (int) $schema->minLength . ' characters long' ); } if ( isset( $schema->maxLength ) && strlen( $data ) > (int) $schema->maxLength ) { $this->add_error( $path, 'must be at most ' . (int) $schema->maxLength . ' characters long' ); } if ( isset( $schema->pattern ) && ! preg_match( '#' . str_replace( '#', '\\#', $schema->pattern ) . '#', $data ) ) { $this->add_error( $path, 'must match the pattern ' . $schema->pattern ); } if ( isset( $schema->format ) && ! $this->matches_format( $data, $schema->format ) ) { $this->add_error( $path, 'must be a valid ' . $schema->format ); } } /** * Apply the number keywords. * * @param int|float $data Value. * @param object $schema Schema node. * @param string $path Path. * @return void */ private function check_number( $data, $schema, $path ) { if ( isset( $schema->minimum ) && $data < $schema->minimum ) { $this->add_error( $path, 'must be greater than or equal to ' . $schema->minimum ); } if ( isset( $schema->maximum ) && $data > $schema->maximum ) { $this->add_error( $path, 'must be less than or equal to ' . $schema->maximum ); } } /** * Apply the array keywords. * * @param array $data Value. * @param object $schema Schema node. * @param string $path Path. * @return void */ private function check_array( $data, $schema, $path ) { if ( isset( $schema->minItems ) && count( $data ) < (int) $schema->minItems ) { $this->add_error( $path, 'must have at least ' . (int) $schema->minItems . ' items' ); } if ( isset( $schema->maxItems ) && count( $data ) > (int) $schema->maxItems ) { $this->add_error( $path, 'must have at most ' . (int) $schema->maxItems . ' items' ); } if ( isset( $schema->items ) && is_object( $schema->items ) ) { foreach ( $data as $index => $item ) { $this->check( $item, $schema->items, $path . '/' . $index ); } } } /** * Apply the object keywords. * * @param object $data Value. * @param object $schema Schema node. * @param string $path Path. * @return void */ private function check_object( $data, $schema, $path ) { if ( isset( $schema->required ) && is_array( $schema->required ) ) { foreach ( $schema->required as $name ) { if ( ! property_exists( $data, $name ) ) { $this->add_error( $path, 'must have the required property ' . $name ); } } } $properties = isset( $schema->properties ) && is_object( $schema->properties ) ? $schema->properties : null; if ( $properties ) { foreach ( get_object_vars( $data ) as $name => $value ) { if ( property_exists( $properties, $name ) ) { $this->check( $value, $properties->{$name}, $path . '/' . $name ); } } } if ( isset( $schema->additionalProperties ) && false === $schema->additionalProperties ) { foreach ( array_keys( get_object_vars( $data ) ) as $name ) { if ( ! $properties || ! property_exists( $properties, $name ) ) { $this->add_error( $path, 'must not have the additional property ' . $name ); } } } } /** * Apply oneOf: exactly one branch must match. * * @param mixed $data Value. * @param array $branches Schema branches. * @param string $path Path. * @return void */ private function check_one_of( $data, array $branches, $path ) { $matched = 0; foreach ( $branches as $branch ) { $probe = new self( $this->root ); $probe->errors = array(); $probe->check( $data, $branch, $path ); if ( empty( $probe->errors ) ) { ++$matched; } } if ( 1 !== $matched ) { $this->add_error( $path, 'must match exactly one of the allowed shapes (matched ' . $matched . ')' ); } } /** * Resolve a local "#/definitions/name" reference. * * @param string $ref Reference. * @return object|null */ private function resolve_ref( $ref ) { if ( 0 !== strpos( $ref, '#/' ) ) { return null; // Remote references are not supported on purpose. } $node = $this->root; foreach ( array_slice( explode( '/', $ref ), 1 ) as $segment ) { $segment = str_replace( array( '~1', '~0' ), array( '/', '~' ), $segment ); if ( ! is_object( $node ) || ! property_exists( $node, $segment ) ) { return null; } $node = $node->{$segment}; } return is_object( $node ) ? $node : null; } /** * Test a value against the "type" keyword. * * @param mixed $data Value. * @param string|array $types Type or list of types. * @return bool */ private function matches_type( $data, $types ) { foreach ( (array) $types as $type ) { switch ( $type ) { case 'object': if ( is_object( $data ) ) { return true; } break; case 'array': if ( is_array( $data ) ) { return true; } break; case 'string': if ( is_string( $data ) ) { return true; } break; case 'integer': // A JSON number with no fractional part is an integer. if ( is_int( $data ) || ( is_float( $data ) && floor( $data ) === $data ) ) { return true; } break; case 'number': if ( ( is_int( $data ) || is_float( $data ) ) && ! is_bool( $data ) ) { return true; } break; case 'boolean': if ( is_bool( $data ) ) { return true; } break; case 'null': if ( null === $data ) { return true; } break; } } return false; } /** * Test the "format" keyword. Only the formats the contract uses. * * @param string $data Value. * @param string $format Format name. * @return bool */ private function matches_format( $data, $format ) { switch ( $format ) { case 'date-time': return (bool) preg_match( '/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})$/', $data ); case 'email': return (bool) preg_match( '/^[^@\s]+@[^@\s]+\.[^@\s]+$/', $data ); case 'uri': return (bool) preg_match( '#^[a-zA-Z][a-zA-Z0-9+.\-]*://[^\s]+$#', $data ); default: return true; // An unknown format never fails validation, per draft-07. } } /** * Record an error. * * @param string $path Path. * @param string $message Message. * @return void */ private function add_error( $path, $message ) { $this->errors[] = ( '' === $path ? 'data' : 'data' . $path ) . ' ' . $message; } } if ( ! function_exists( 'aha_is_woocommerce_json_encode_compat' ) ) { /** * Encode a scalar for an error message. * * The plugin cannot call wp_json_encode() from the test runner, which loads * this file without WordPress. * * @param mixed $value Value. * @return string */ function aha_is_woocommerce_json_encode_compat( $value ) { return is_scalar( $value ) ? (string) $value : gettype( $value ); } }