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

Add nested objects support #8

Merged
merged 3 commits into from
Dec 18, 2019

Conversation

Sergej-Popov
Copy link
Collaborator

@Sergej-Popov Sergej-Popov commented Dec 17, 2019

Adds support for nested objects, this allows to create a schema for arbitrary dictionary objects where the nested structure is defined but keys are not. e.g OpenAPI spec

  "paths": {
    "/my-resource": {
      "post": {
        "tags": [
          "resource"
        ],
        "summary": "Create my Resource"

In the above example, resource structure is well defined, but the path identifiers are not.

The second commit in this PR adds support for double nested dictionaries. e.g:

{
      NestedDictionaryProp: {
        "KeyA": {
          "KeyA1": {
            DictionaryChildNumberProp: 500
          }
        }
      }
    }

Type of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

All Submissions:

  • Have you followed the guidelines in our Contributing document?
  • Have you checked to ensure there aren't other open Pull Requests for the same update/change?
  • I have updated the documentation accordingly.

New Feature Submissions:

  • Does your submission pass tests?
  • Have you lint your code locally prior to submission?

Changes to Core Features:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your core changes, as applicable?
  • Have you successfully ran tests with your changes locally?

Copy link
Contributor

@bingnz bingnz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! One suggestion I have is that perhaps you could wrap the schema objects using more fluent static 'factories'. For example,

const schema = new Schema<Model>()
        .with(m => m.StringProp, new AnyOf([
          "specificValueNotMatchingRegex",
          /^[A-z]+\.[A-z]+$/,
          new StringSchema({
            enum: ["123"]
          })
        ]))
        .build();

could be expressed as something like

      const schema = new Schema<Model>()
        .with(m => m.StringProp, jsonschema.matching.anyOf([
          "specificValueNotMatchingRegex",
          /^[A-z]+\.[A-z]+$/,
          jsonschema.string.exactMatch("123")
        ]))
        .build();

.github/pull_request_template.md Outdated Show resolved Hide resolved
tests/object.spec.ts Outdated Show resolved Hide resolved
tests/object.spec.ts Outdated Show resolved Hide resolved
@Sergej-Popov
Copy link
Collaborator Author

I like static factories idea. I will create an issue to track that separately.

@Sergej-Popov Sergej-Popov merged commit 99b75d9 into justeat:master Dec 18, 2019
@Sergej-Popov Sergej-Popov mentioned this pull request Jan 30, 2020
11 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants