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

Does not validate from external schema #38

Closed
radio-miskovice opened this issue Jul 26, 2017 · 7 comments
Closed

Does not validate from external schema #38

radio-miskovice opened this issue Jul 26, 2017 · 7 comments

Comments

@radio-miskovice
Copy link

When I try to compile the attached schema comment.schema.json, it fails with error message

schema comment.schema.json is invalid
error: can't resolve reference user.schema.json from id #

Both comment.schema.json and user.schema.json are located in the same directory, where also ajv is invoked.

The same schema validates with jayschema (which is what is to be expected because the respective $ref value does not contain any # character, so it does not refer to the current schema document).

I also tried "./user.schema.json" with the same result: does not validate in ajv but it does in jayschema.

@radio-miskovice
Copy link
Author

radio-miskovice commented Jul 26, 2017

user.schema.json

{
  "title": "User Object",
  "description": "User Object used in FCT website app",
  "definitions": {
    "integer": {
      "type": "number",
      "multipleOf": 1
    },
    "identifierInt": {
      "type": "number",
      "multipleOf": 1,
      "minimum": 0
    }
  },
  "type": "object",
  "required": [
    "id",
    "alias",
    "role",
    "avatar"
  ],
  "properties": {
    "id": {
      "description": "Source ID of the user (numeric)",
      "$ref": "#/definitions/identifierInt"
    },
    "alias": {
      "description": "User alias to be displayed on the website",
      "type": "string"
    },
    "role": {
      "description": "User role in the FCT site, in the format for display on the website",
      "type": "string"
    },
    "avatar": {
      "description": "absolute or relative link to user's avatar image (usable in <img src=...> attribute)",
      "type": "string",
      "format": "uri"
    }
  }
}

@radio-miskovice
Copy link
Author

radio-miskovice commented Jul 26, 2017

comment.schema.json

{
  "title": "Comment",
  "description": "Comment object schema",
  "definitions": {
    "integer": {
      "type": "number",
      "multipleOf": 1,
      "minimum": 0
    }
  },
  "type": "object",
  "required": [
    "id",
    "user",
    "text",
    "datetime"
  ],
  "properties": {
    "id": {
      "description": "Source identifier of the comment item (database table key)",
      "$ref": "#/definitions/integer"
    },
    "questionId": {
      "description": "Source identifier (database key) of the related question",
      "$ref": "#/definitions/integer"
    },
    "repliesTo": {
      "description": "Source identifier of the comment being replied to",
      "$ref": "#/definitions/integer"
    },
    "user": {
      "description": "User object containing data of the user who entered the comment",
      "$ref": "user.schema.json"
    },
    "text": {
      "description": "Text of the question",
      "type": "string"
    },
    "datetime": {
      "description": "Question timestamp (entry time)",
      "type": "string",
      "format": "datetime"
    }
  }
}

@epoberezkin
Copy link
Member

ajv-cli does not look up files on the file system unless you tell it about them - you need to use -r option.

What is the command you use to compile schema?

@radio-miskovice
Copy link
Author

C:\DEV\_FKT_\site\schemas>ajv compile -s user.schema.json
schema user.schema.json is valid

C:\DEV\_FKT_\site\schemas>ajv compile -s comment.schema.json
schema comment.schema.json is invalid
error: can't resolve reference user.schema.json from id #

@epoberezkin
Copy link
Member

as I wrote, you need to use -r option:

ajv compile -s comment.schema.json -r user.schema.json

and user.schema.json needs to have id attribute.

Ajv itself allows adding schema id as a second parameter of .addSchema method, but ajv-cli only supports id attribute as part of the schema.

@radio-miskovice
Copy link
Author

I understand that this is not a feature of the software, but I wonder what is then the meaning of having command line automation tool and $ref references if I have to find and declare all external schemas manually...
Jayschema is much better for model validation, unfortunately a little bit out of date.

@epoberezkin
Copy link
Member

You can -r the whole folder using glob patterns

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

2 participants