Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update @JsonSchema #112

Merged
merged 10 commits into from
Jul 27, 2017
Merged

Conversation

koriym
Copy link
Member

@koriym koriym commented Jul 27, 2017

  • scheme json file location by CoC is deprecated.
  • specify json_schema and json_validate folder in JsonSchemalModule
  • @jsonschema::$schema prop to locate schema file
  • @jsonschema::$params to locate request validation schema json file
  • @jsonschema::$key for body array key

Install module

$this->install(new JsonSchemalModule($appDir . '/var/json_schema', $appDir . '/var/json_validate'));

Put scheme files under specified directories.

/var/json_schema/todo.json

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "Todo schema",
  "description": "Todo schema that should be used to model todo application.",
  "type": "object",
  "properties": {
    "title": {
      "type": "string",
      "minLength": 1,
      "maxLength": 40
    },
    "status": {
      "enum": ["1", "2"]
    },
    "created": {
      "type": "string",
      "format": "datetime"
    },
    "updated": {
      "type": "string",
      "format": "datetime"
    }
  },
  "required": ["title", "status", "created", "updated"],
  "additionalProperties": true
}

/var/json_validate/todo.post.json

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "/todo POST request validation",
  "properties": {
    "title": {
      "type": "string",
      "minLength": 1,
      "maxLength": 40
    }
}

Annotate @JsonScheme for input parameters or (and) response validation.

response validation

    /**
     * @JsonSchema(key="todo", schema="todo.json")
     */
    public function onGet(string $id) : ResourceObject

request validation

    /**
     * @JsonSchema(params="todo.post.json")
     */
    public function onPost(string $title) : ResourceObject

key is optional when body contains certain key. ($this->body[$key] = [...];)
Properties (key, schema and request) can be combined in one @jsonschema annotation.

Older API still valid with new PR. Backward compatible.

@koriym koriym added the Feature label Jul 27, 2017
@koriym koriym changed the title Update @JsonScheme Update @JsonSchema Jul 27, 2017
@koriym koriym merged commit 188fefb into bearsunday:1.x Jul 27, 2017
@koriym koriym deleted the json-scheme-request-validate branch July 27, 2017 18:07
@koriym koriym restored the json-scheme-request-validate branch July 28, 2017 03:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant