| 1 | <?php |
| 2 | |
| 3 | namespace MediaWiki\ParamValidator\TypeDef; |
| 4 | |
| 5 | use InvalidArgumentException; |
| 6 | use JsonSchema\Constraints\Constraint; |
| 7 | use JsonSchema\Validator; |
| 8 | use LogicException; |
| 9 | use Wikimedia\Message\DataMessageValue; |
| 10 | use Wikimedia\ParamValidator\TypeDef; |
| 11 | |
| 12 | /** |
| 13 | * Type definition for array structures, typically |
| 14 | * used for validating JSON request bodies. |
| 15 | * |
| 16 | * Failure codes: |
| 17 | * - 'notarray': The value is not an array. |
| 18 | * |
| 19 | * @since 1.42 |
| 20 | */ |
| 21 | class ArrayDef extends TypeDef { |
| 22 | |
| 23 | /** |
| 24 | * (object) Schema settings. |
| 25 | * |
| 26 | */ |
| 27 | public const PARAM_SCHEMA = 'param-schema'; |
| 28 | |
| 29 | public function supportsArrays() { |
| 30 | return true; |
| 31 | } |
| 32 | |
| 33 | public function validate( $name, $value, array $settings, array $options ) { |
| 34 | if ( !is_array( $value ) ) { |
| 35 | // Message used: paramvalidator-notarray |
| 36 | $this->failure( 'notarray', $name, $value, $settings, $options ); |
| 37 | } |
| 38 | |
| 39 | if ( isset( $settings[ self::PARAM_SCHEMA ] ) ) { |
| 40 | $schema = $settings[ self::PARAM_SCHEMA ]; |
| 41 | |
| 42 | if ( !isset( $schema[ 'type' ] ) ) { |
| 43 | throw new InvalidArgumentException( "Schema type not set " ); |
| 44 | } |
| 45 | |
| 46 | $types = (array)$schema['type']; |
| 47 | foreach ( $types as $type ) { |
| 48 | // @todo: start using JsonSchemaTrait::normalizeJsonSchema |
| 49 | // so we can also support the "list" and "map" types |
| 50 | if ( ( $type !== 'object' ) && ( $type !== 'array' ) ) { |
| 51 | throw new LogicException( 'Invalid data type' ); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | $validator = new Validator(); |
| 56 | $validator->validate( |
| 57 | $value, $schema, |
| 58 | Constraint::CHECK_MODE_TYPE_CAST | Constraint::CHECK_MODE_APPLY_DEFAULTS ); |
| 59 | if ( !$validator->isValid() ) { |
| 60 | $errorCode = 'schema-validation-failed'; |
| 61 | foreach ( $validator->getErrors() as $error ) { |
| 62 | $message = DataMessageValue::new( |
| 63 | "paramvalidator-$errorCode", |
| 64 | [ $error[ 'message' ] ], |
| 65 | $errorCode, |
| 66 | [ 'schema-validation-error' => $error ] |
| 67 | ); |
| 68 | $this->failure( $message, $name, $value, $settings, $options ); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | return $value; |
| 73 | } |
| 74 | |
| 75 | public function stringifyValue( $name, $value, array $settings, array $options ) { |
| 76 | if ( !is_array( $value ) ) { |
| 77 | return parent::stringifyValue( $name, $value, $settings, $options ); |
| 78 | } |
| 79 | |
| 80 | return json_encode( $value ); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Returns a JSON Schema of type array, with the input schema for each array item. |
| 85 | * |
| 86 | * If $itemSchema is a string, it must be a valid JSON type, and all list entries will be |
| 87 | * validated to be of that type. |
| 88 | * |
| 89 | * If $itemSchema is an array, it must represent a valid schema, and all list entries will |
| 90 | * be validated to be of that schema. Nested lists are supported, as are lists of maps and |
| 91 | * more complicated schemas. |
| 92 | * |
| 93 | * Examples: |
| 94 | * A list of integers, like [ 1, 2, 3 ] |
| 95 | * ArrayDef::makeListSchema( "integer" ) |
| 96 | * A list of strings, where each value must be either "a" or "b", like [ "a", "a", "b", "b" ] |
| 97 | * * ArrayDef::makeListSchema( [ 'enum' => [ 'a', 'b' ] ] ) |
| 98 | * A list of lists of strings, like [ [ "foo", 'bar" ], [ "baz", "qux" ] ] |
| 99 | * ArrayDef::makeListSchema( ArrayDef::makeListSchema( "string" ) ) |
| 100 | * |
| 101 | * @since 1.43 |
| 102 | * |
| 103 | * @param array|string $itemSchema |
| 104 | * |
| 105 | * @return array |
| 106 | */ |
| 107 | public static function makeListSchema( $itemSchema ): array { |
| 108 | return [ |
| 109 | 'type' => 'array', |
| 110 | 'items' => static::normalizeSchema( $itemSchema ) |
| 111 | ]; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Returns a JSON Schema of type object, with the input schema for each array item. |
| 116 | * |
| 117 | * If $entrySchema is a string, it must be a valid JSON type, and all map entries will be |
| 118 | * validated to be of that type. |
| 119 | * |
| 120 | * If $entrySchema is an array, it must represent a valid schema, and all map entries will |
| 121 | * be validated to be of that schema. Nested maps are supported, as are maps of lists and |
| 122 | * more complicated schemas |
| 123 | * |
| 124 | * Examples: |
| 125 | * A map of integers, like [ 'key1' => 1, 'key2' => 2, 'key3' => 3 ] |
| 126 | * ArrayDef::makeMapSchema( "integer" ) |
| 127 | * A map of where each value must be 0 or 1, like [ 'key1' => 1, 'key2' => 1, 'key3' => 0 ] |
| 128 | * ArrayDef::makeMapSchema( [ 'enum' => [ 0, 1 ] ] ) |
| 129 | * A map of maps, like [ 'k1' => [ 'k2' => 'a' ], 'k3' => [ 'k4' => 'b', 'k5' => 'c' ] ] |
| 130 | * ArrayDef::makeMapSchema( ArrayDef::makeMapSchema( "string" ) ) |
| 131 | * |
| 132 | * @since 1.43 |
| 133 | * |
| 134 | * @param array|string $entrySchema |
| 135 | * |
| 136 | * @return array |
| 137 | */ |
| 138 | public static function makeMapSchema( $entrySchema ): array { |
| 139 | return [ |
| 140 | 'type' => 'object', |
| 141 | 'additionalProperties' => static::normalizeSchema( $entrySchema ) |
| 142 | ]; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Returns a JSON Schema of type object, with properties defined by the function params. |
| 147 | * |
| 148 | * Any input schemas must either be a string corresponding to valid JSON types, or valid |
| 149 | * schemas. Nested schemas are supported. |
| 150 | * |
| 151 | * Examples: |
| 152 | * An object with required parameters "a" and "b", where "a" must be an integer and "b" can |
| 153 | * have one of the values "x", "y", or "z", no optional parameters, and additional parameters |
| 154 | * are disallowed: |
| 155 | * ArrayDef::makeObjectSchema( [ 'a' => 'integer', 'b' => [ 'enum' => [ 'x', 'y', 'z' ] ] ] ) |
| 156 | * The same object, but parameter "b" is optional: |
| 157 | * ArrayDef::makeObjectSchema( [ 'a' => 'integer' ], [ 'b' => [ 'enum' => [ 'x', 'y', 'z' ] ] ] ) |
| 158 | * An object with no required properties, an optional property "a" of type string, with |
| 159 | * arbitrary additional properties allowed (effectively, an arbitrary object, but if "a" |
| 160 | * is present, it must be a string): |
| 161 | * ArrayDef::makeObjectSchema( [ ], [ 'a' => 'string' ], true ) |
| 162 | * @since 1.43 |
| 163 | * |
| 164 | * @param array $required properties that are required to be present, as name/schema pairs |
| 165 | * @param array $optional properties that may or may not be present, as name/schema pairs |
| 166 | * @param array|bool|string $additional schema additional properties must match, or false |
| 167 | * to disallow additional properties, or true to allow arbitrary additional properties |
| 168 | * |
| 169 | * @return array |
| 170 | */ |
| 171 | public static function makeObjectSchema( |
| 172 | array $required = [], array $optional = [], $additional = false |
| 173 | ): array { |
| 174 | $schema = [ 'type' => 'object' ]; |
| 175 | |
| 176 | if ( $required ) { |
| 177 | foreach ( $required as $propertyName => $propertySchema ) { |
| 178 | $schema['required'][] = $propertyName; |
| 179 | $schema['properties'][$propertyName] = static::normalizeSchema( $propertySchema ); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | if ( $optional ) { |
| 184 | foreach ( $optional as $propertyName => $propertySchema ) { |
| 185 | if ( isset( $schema['properties'][$propertyName] ) ) { |
| 186 | throw new InvalidArgumentException( |
| 187 | "Property {$propertyName} defined as both required and optional" |
| 188 | ); |
| 189 | } |
| 190 | $schema['properties'][$propertyName] = static::normalizeSchema( $propertySchema ); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | // The easiest way to allow all extra properties is to not specify additionalProperties |
| 195 | if ( $additional !== true ) { |
| 196 | // A value of false disallows additional properties |
| 197 | if ( $additional === false ) { |
| 198 | $schema['additionalProperties'] = false; |
| 199 | } else { |
| 200 | $schema['additionalProperties'] = static::normalizeSchema( $additional ); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | return $schema; |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Returns a representation of the input schema |
| 209 | * |
| 210 | * If $schema is a string, it must be a valid JSON type. |
| 211 | * If $schema is an array, it must represent a valid schema. |
| 212 | * |
| 213 | * @param array|string $schema |
| 214 | * |
| 215 | * @return array |
| 216 | */ |
| 217 | private static function normalizeSchema( $schema ): array { |
| 218 | if ( is_array( $schema ) ) { |
| 219 | return $schema; |
| 220 | } else { |
| 221 | return [ 'type' => $schema ]; |
| 222 | } |
| 223 | } |
| 224 | } |