Giter Club home page Giter Club logo

Comments (10)

whitlockjc avatar whitlockjc commented on August 10, 2024

I guess it could be related to #37 as well since this might be a naming issue.

from z-schema.

zaggino avatar zaggino commented on August 10, 2024

Fixed by c27e149

from z-schema.

whitlockjc avatar whitlockjc commented on August 10, 2024

Thanks.

from z-schema.

whitlockjc avatar whitlockjc commented on August 10, 2024

I tested this and with a contrived example like the one above, the trailing slashes do not seem to be a big deal. But if I use the following example, it fails again (Disregard the one for the draft spec, my code above doesn't wire it in for brevity):

var dataTypeBaseJson = {
  "id": "http://wordnik.github.io/schemas/v1.2/dataTypeBase.json#",
  "$schema": "http://json-schema.org/draft-04/schema#",
  "description": "Data type fields (section 4.3.3)",
  "type": "object",
  "oneOf": [
    {
      "required": [
        "type"
      ]
    },
    {
      "required": [
        "$ref"
      ]
    }
  ],
  "properties": {
    "type": {
      "type": "string"
    },
    "$ref": {
      "type": "string"
    },
    "format": {
      "type": "string"
    },
    "defaultValue": {
      "not": {
        "type": [
          "array",
          "object",
          "null"
        ]
      }
    },
    "enum": {
      "type": "array",
      "items": {
        "type": "string"
      },
      "uniqueItems": true,
      "minItems": 1
    },
    "minimum": {
      "type": "string"
    },
    "maximum": {
      "type": "string"
    },
    "items": {
      "$ref": "#/definitions/itemsObject"
    },
    "uniqueItems": {
      "type": "boolean"
    }
  },
  "dependencies": {
    "format": {
      "oneOf": [
        {
          "properties": {
            "type": {
              "enum": [
                "integer"
              ]
            },
            "format": {
              "enum": [
                "int32",
                "int64"
              ]
            }
          }
        },
        {
          "properties": {
            "type": {
              "enum": [
                "number"
              ]
            },
            "format": {
              "enum": [
                "float",
                "double"
              ]
            }
          }
        },
        {
          "properties": {
            "type": {
              "enum": [
                "string"
              ]
            },
            "format": {
              "enum": [
                "byte",
                "date",
                "date-time"
              ]
            }
          }
        }
      ]
    }
  },
  "definitions": {
    "itemsObject": {
      "oneOf": [
        {
          "type": "object",
          "required": [
            "$ref"
          ],
          "properties": {
            "$ref": {
              "type": "string"
            }
          },
          "additionalProperties": false
        },
        {
          "allOf": [
            {
              "$ref": "#"
            },
            {
              "required": [
                "type"
              ],
              "properties": {
                "type": {},
                "format": {}
              },
              "additionalProperties": false
            }
          ]
        }
      ]
    }
  }
};
var modelsObjectJson = {
  "id": "http://wordnik.github.io/schemas/v1.2/modelsObject.json#",
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "required": [
    "id",
    "properties"
  ],
  "properties": {
    "id": {
      "type": "string"
    },
    "description": {
      "type": "string"
    },
    "properties": {
      "type": "object",
      "additionalProperties": {
        "$ref": "#/definitions/propertyObject"
      }
    },
    "subTypes": {
      "type": "array",
      "items": {
        "type": "string"
      },
      "uniqueItems": true
    },
    "discriminator": {
      "type": "string"
    }
  },
  "dependencies": {
    "subTypes": [
      "discriminator"
    ]
  },
  "definitions": {
    "propertyObject": {
      "allOf": [
        {
          "not": {
            "$ref": "#"
          }
        },
        {
          "$ref": "dataTypeBase.json#"
        }
      ]
    }
  }
};
var oauth2GrantTypeJson = {
  "id": "http://wordnik.github.io/schemas/v1.2/oauth2GrantType.json#",
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "minProperties": 1,
  "properties": {
    "implicit": {
      "$ref": "#/definitions/implicit"
    },
    "authorization_code": {
      "$ref": "#/definitions/authorizationCode"
    }
  },
  "definitions": {
    "implicit": {
      "type": "object",
      "required": [
        "loginEndpoint"
      ],
      "properties": {
        "loginEndpoint": {
          "$ref": "#/definitions/loginEndpoint"
        },
        "tokenName": {
          "type": "string"
        }
      },
      "additionalProperties": false
    },
    "authorizationCode": {
      "type": "object",
      "required": [
        "tokenEndpoint",
        "tokenRequestEndpoint"
      ],
      "properties": {
        "tokenEndpoint": {
          "$ref": "#/definitions/tokenEndpoint"
        },
        "tokenRequestEndpoint": {
          "$ref": "#/definitions/tokenRequestEndpoint"
        }
      },
      "additionalProperties": false
    },
    "loginEndpoint": {
      "type": "object",
      "required": [
        "url"
      ],
      "properties": {
        "url": {
          "type": "string",
          "format": "uri"
        }
      },
      "additionalProperties": false
    },
    "tokenEndpoint": {
      "type": "object",
      "required": [
        "url"
      ],
      "properties": {
        "url": {
          "type": "string",
          "format": "uri"
        },
        "tokenName": {
          "type": "string"
        }
      },
      "additionalProperties": false
    },
    "tokenRequestEndpoint": {
      "type": "object",
      "required": [
        "url"
      ],
      "properties": {
        "url": {
          "type": "string",
          "format": "uri"
        },
        "clientIdName": {
          "type": "string"
        },
        "clientSecretName": {
          "type": "string"
        }
      },
      "additionalProperties": false
    }
  }
};
var authorizationObjectJson = {
  "id": "http://wordnik.github.io/schemas/v1.2/authorizationObject.json#",
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "additionalProperties": {
    "oneOf": [
      {
        "$ref": "#/definitions/basicAuth"
      },
      {
        "$ref": "#/definitions/apiKey"
      },
      {
        "$ref": "#/definitions/oauth2"
      }
    ]
  },
  "definitions": {
    "basicAuth": {
      "required": [
        "type"
      ],
      "properties": {
        "type": {
          "enum": [
            "basicAuth"
          ]
        }
      },
      "additionalProperties": false
    },
    "apiKey": {
      "required": [
        "type",
        "passAs",
        "keyname"
      ],
      "properties": {
        "type": {
          "enum": [
            "apiKey"
          ]
        },
        "passAs": {
          "enum": [
            "header",
            "query"
          ]
        },
        "keyname": {
          "type": "string"
        }
      },
      "additionalProperties": false
    },
    "oauth2": {
      "type": "object",
      "required": [
        "type",
        "grantTypes"
      ],
      "properties": {
        "type": {
          "enum": [
            "oauth2"
          ]
        },
        "scopes": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/oauth2Scope"
          }
        },
        "grantTypes": {
          "$ref": "oauth2GrantType.json#"
        }
      },
      "additionalProperties": false
    },
    "oauth2Scope": {
      "type": "object",
      "required": [
        "scope"
      ],
      "properties": {
        "scope": {
          "type": "string"
        },
        "description": {
          "type": "string"
        }
      },
      "additionalProperties": false
    }
  }
};
var parameterObjectJson = {
  "id": "http://wordnik.github.io/schemas/v1.2/parameterObject.json#",
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "allOf": [
    {
      "$ref": "dataTypeBase.json#"
    },
    {
      "required": [
        "paramType",
        "name"
      ],
      "properties": {
        "paramType": {
          "enum": [
            "path",
            "query",
            "body",
            "header",
            "form"
          ]
        },
        "name": {
          "type": "string"
        },
        "description": {
          "type": "string"
        },
        "required": {
          "type": "boolean"
        },
        "allowMultiple": {
          "type": "boolean"
        }
      }
    },
    {
      "description": "type File requires special paramType and consumes",
      "oneOf": [
        {
          "properties": {
            "type": {
              "not": {
                "enum": [
                  "File"
                ]
              }
            }
          }
        },
        {
          "properties": {
            "type": {
              "enum": [
                "File"
              ]
            },
            "paramType": {
              "enum": [
                "form"
              ]
            },
            "consumes": {
              "enum": [
                "multipart/form-data"
              ]
            }
          }
        }
      ]
    }
  ]
};
var operationObjectJson = {
  "id": "http://wordnik.github.io/schemas/v1.2/operationObject.json#",
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "allOf": [
    {
      "$ref": "dataTypeBase.json#"
    },
    {
      "required": [
        "method",
        "nickname",
        "parameters"
      ],
      "properties": {
        "method": {
          "enum": [
            "GET",
            "POST",
            "PUT",
            "PATCH",
            "DELETE",
            "OPTIONS"
          ]
        },
        "summary": {
          "type": "string",
          "maxLength": 120
        },
        "notes": {
          "type": "string"
        },
        "nickname": {
          "type": "string",
          "pattern": "^[a-zA-Z0-9_]+$"
        },
        "authorizations": {
          "type": "object",
          "additionalProperties": {
            "type": "array",
            "items": {
              "$ref": "authorizationObject.json#/definitions/oauth2Scope"
            }
          }
        },
        "parameters": {
          "type": "array",
          "items": {
            "$ref": "parameterObject.json#"
          }
        },
        "responseMessages": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/responseMessageObject"
          }
        },
        "produces": {
          "$ref": "#/definitions/mimeTypeArray"
        },
        "consumes": {
          "$ref": "#/definitions/mimeTypeArray"
        },
        "deprecated": {
          "enum": [
            "true",
            "false"
          ]
        }
      }
    }
  ],
  "definitions": {
    "responseMessageObject": {
      "type": "object",
      "required": [
        "code",
        "message"
      ],
      "properties": {
        "code": {
          "$ref": "#/definitions/rfc2616section10"
        },
        "message": {
          "type": "string"
        },
        "responseModel": {
          "type": "string"
        }
      }
    },
    "rfc2616section10": {
      "type": "integer",
      "minimum": 100,
      "maximum": 600,
      "exclusiveMaximum": true
    },
    "mimeTypeArray": {
      "type": "array",
      "items": {
        "type": "string",
        "format": "mime-type"
      }
    }
  }
}
var apiDeclarationJson = {
  "id": "http://wordnik.github.io/schemas/v1.2/apiDeclaration.json#",
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "required": [
    "swaggerVersion",
    "basePath",
    "apis"
  ],
  "properties": {
    "swaggerVersion": {
      "enum": [
        "1.2"
      ]
    },
    "apiVersion": {
      "type": "string"
    },
    "basePath": {
      "type": "string",
      "format": "uri",
      "pattern": "^https?://"
    },
    "resourcePath": {
      "type": "string",
      "format": "uri",
      "pattern": "^/"
    },
    "apis": {
      "type": "array",
      "items": {
        "$ref": "#/definitions/apiObject"
      }
    },
    "models": {
      "type": "object",
      "additionalProperties": {
        "$ref": "modelsObject.json#"
      }
    },
    "produces": {
      "$ref": "#/definitions/mimeTypeArray"
    },
    "consumes": {
      "$ref": "#/definitions/mimeTypeArray"
    },
    "authorizations": {
      "$ref": "authorizationObject.json#"
    }
  },
  "additionalProperties": false,
  "definitions": {
    "apiObject": {
      "type": "object",
      "required": [
        "path",
        "operations"
      ],
      "properties": {
        "path": {
          "type": "string",
          "format": "uri-template",
          "pattern": "^/"
        },
        "description": {
          "type": "string"
        },
        "operations": {
          "type": "array",
          "items": {
            "$ref": "operationObject.json#"
          }
        }
      },
      "additionalProperties": false
    },
    "mimeTypeArray": {
      "type": "array",
      "items": {
        "type": "string",
        "format": "mime-type"
      }
    }
  }
};
var ZSchema = require('z-schema');
var validator = new ZSchema();
var result = validator.compileSchema([
  dataTypeBaseJson,
  modelsObjectJson,
  oauth2GrantTypeJson,
  authorizationObjectJson,
  parameterObjectJson,
  operationObjectJson,
  apiDeclarationJson
]);

if (result === false) {
  console.dir(validator.getLastErrors());
}

I realize this is likely due to the schema id being a fully qualified URL and the $ref is a file name at best but when I try to work around it by making the schema id match the file name (with trailing slash) so that the references should resolve, I get the same type of errors:

[ { code: 'UNRESOLVABLE_REFERENCE',
    params: [ 'modelsObject.json#' ],
    message: 'Reference could not be resolved: modelsObject.json#',
    path: '#/properties/models/additionalProperties' },
  { code: 'UNRESOLVABLE_REFERENCE',
    params: [ 'dataTypeBase.json#' ],
    message: 'Reference could not be resolved: dataTypeBase.json#',
    path: '#/allOf/0' },
  { code: 'UNRESOLVABLE_REFERENCE',
    params: [ 'dataTypeBase.json#' ],
    message: 'Reference could not be resolved: dataTypeBase.json#',
    path: '#/allOf/0' },
  { code: 'UNRESOLVABLE_REFERENCE',
    params: [ 'oauth2GrantType.json#' ],
    message: 'Reference could not be resolved: oauth2GrantType.json#',
    path: '#/definitions/oauth2/properties/grantTypes' },
  { code: 'UNRESOLVABLE_REFERENCE',
    params: [ 'http://json-schema.org/draft-04/schema#' ],
    message: 'Reference could not be resolved: http://json-schema.org/draft-04/schema#',
    path: '#/' },
  { code: 'UNRESOLVABLE_REFERENCE',
    params: [ 'dataTypeBase.json#' ],
    message: 'Reference could not be resolved: dataTypeBase.json#',
    path: '#/definitions/propertyObject/allOf/1' },
  { code: 'UNRESOLVABLE_REFERENCE',
    params: [ 'http://json-schema.org/draft-04/schema#' ],
    message: 'Reference could not be resolved: http://json-schema.org/draft-04/schema#',
    path: '#/' } ]

I even tried without the trailing slashes in the schema id and I still get the same errors.

from z-schema.

zaggino avatar zaggino commented on August 10, 2024

@whitlockjc you need to resolve http://json-schema.org/draft-04/schema manually like this:

validator.setRemoteReference("http://json-schema.org/draft-04/schema", require("../files/draft-04-schema.json"));

or read more here https://github.com/zaggino/z-schema#remote-references-and-schemas

from z-schema.

zaggino avatar zaggino commented on August 10, 2024

The thing is, some of your schemas are not able to compile due to missing http://json-schema.org/draft-04/schema and then other of your schemas are not able to reference these schemas too.

from z-schema.

whitlockjc avatar whitlockjc commented on August 10, 2024

I know how to do that. :) I just didn't know omitting it would break these others, I figured they'd just show it as a missing reference. My attempt to be brief to help use a real world example might be what's causing my problem. sigh

from z-schema.

whitlockjc avatar whitlockjc commented on August 10, 2024

That was it. Once I included the draft spec and then compiled the schemas, it worked fine now.

from z-schema.

whitlockjc avatar whitlockjc commented on August 10, 2024

Disregard my last (deleted) comments, everything is working perfectly now. :) I will plug this into swagger-tools and see how far I get. I will also get cracking on the PR for #55.

from z-schema.

zaggino avatar zaggino commented on August 10, 2024

Cool :)

from z-schema.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.