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

Nullable true not being read on object #983

Closed
Tyharo1 opened this issue Apr 2, 2019 · 3 comments
Closed

Nullable true not being read on object #983

Tyharo1 opened this issue Apr 2, 2019 · 3 comments

Comments

@Tyharo1
Copy link
Contributor

Tyharo1 commented Apr 2, 2019

Hello, I'm using AJV to validate a response object to a OpenApi3.0 schema. Within the project we built a method thats a pass through to AJV called "schemaValdiator". The issue we ran into is that the nullable attribute doesn't appear to be read when nested in larges schemas. This results in an like described below. Even though nullable is set to true the validate method returns an error regarding the type. Changing "type": "object", to "type": ["object", "null"], will fix the issue but this feels hacky as AJV should read the "nullable": true attribute.

Version: 6.10.0

TOP LEVEL JSON Schema

{
  "type": "object",
  "required": ["propertiesOwned", "_links"],
  "properties": {
    "propertiesOwned": {
      "type": "array",
      "items": {
        "required": ["address"],
        "type": "object",
        "properties": {
          "address": {
            "description": "Object containing properties regarding the clients address.",
            "nullable": true,
            "type": "object",
            "required": ["street", "street2", "city", "state", "zipCode"],
            "properties": {
              "street": {
                "description": "Primary street of subject property address",
                "type": "string",
                "example": "1234 street"
              },
              "street2": {
                "description": "Secondary street of subject property address",
                "type": "string",
                "nullable": true,
                "example": "Apt. 1234"
              },
              "city": {
                "description": "City of subject property address",
                "type": "string",
                "example": "SomeCity"
              },
              "state": {
                "description": "State of subject property address",
                "type": "string",
                "example": "MI"
              },
              "zipCode": {
                "description": "ZipCode of subject property address",
                "type": "string",
                "example": "48124"
              },
              "county": {
                "description": "County of subject property address",
                "type": "string",
                "nullable": true,
                "example": "Wayne"
              },
              "countyFIPS": {
                "description": "CountyFIPS of subject property address",
                "type": "string",
                "nullable": true,
                "example": "26163"
              },
              "addressId": {
                "description": "ID assigned to address",
                "type": "string",
                "nullable": true,
                "example": "221133"
              }
            }
          }
        }
      }
    },
    "_links": {
      "description": "List of links for next RM steps (RM only)",
      "type": "array",
      "example": [
        {
          "rel": "self",
          "href": "http://localhost:8080/loanApplications/1234/refinanceGoal"
        }
      ]
    }
  }
}

Sample data

{
  "propertiesOwned": [
    {
      "address": {
        "street": "123 My Street",
        "city": "Ann Arbor",
        "state": "MI",
        "zipCode": "47001",
        "street2": null,
        "county": "Wayne",
        "countyFIPS": "999"
      }
    }
  ],
  "_links": [
    {
      "rel": "self",
      "href": "http://localhost:8080/loanApplications/bb6e21e0-e4d8-460a-91b2-157fb1a11952/propertiesOwned"
    }
  ]
}

Your code

const validator = (schema: string, schemaName: string): SchemaValidator => {
    return {
        validate(object: {}) {
            return $RefParser.dereference(schema, dereferencerConfig)
                .then((dereferencedSchema: {}) => {

                    const validate: ajv.ValidateFunction = ajv().compile(dereferencedSchema);

                    validate(object);

                    if (validate.errors) {
                        const message = validate.errors.map((error) => inspect(error, false, 100)).join();
                        throw new Error(schemaName + message);
                    }
                });
        },
    };
};

Validation result, data AFTER validation, error messages

Error: getPropertiesOwnedResponse{ keyword: 'type',
  dataPath: '.propertiesOwned[0].address',
  schemaPath: '#/properties/propertiesOwned/items/properties/address/type',
  params: { type: 'object' },
  message: 'should be object' }
    at $RefParser.dereference.then (shared-libs/build/schemaValidator/schemaValidator.js:39:27)
    at <anonymous>

What results did you expect?
Schema to be validated successfully.

@Tyharo1
Copy link
Contributor Author

Tyharo1 commented Apr 2, 2019

I found the solution to this issue and hopefully this will help anyone else with this issue. So the option to use nullable is false by default see here (I wasn't ware). To enable it pass {nullable: true} as a param to the AJV instance, see example below.

const validate: ajv.ValidateFunction = ajv({nullable: true}).compile(dereferencedSchema);

Sorry for the noise, hopefully this is still helpful to anyone seeing this as well.

@railgunCatt
Copy link

Wow, thanks a lot this. Came in clutch

@MilanJimi
Copy link

Seems that the posted solution is no longer up to date in 8.12.0.

Is there any alternative? I am still using Openapi 3.0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants