Search code examples
jsonjsonschema

JSON Schema to validate whether or not a file path exists


I am writing a JSON schema, and I want to add a field that takes in a file path. Is there a way to validate if the file path exists (Similar to how $schema displays an error when it cannot find the schema file)? Here is what I have:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "properties": {
    "file": {
      "type": "string",
      "pattern": "^(\\./|\\.\\./|https?://).*\\.json$",
      "description": "The path to another json file."
    }
  }
}

It would look like this in a file, and if the file does not exist, it would display a warning/error.

{
  "file": "../path/to/file.json"
}

File path not found.


Solution

  • First thing is $schema doesn't resolve to anything. It's an identifier of the meta-schema, not a network reachable uri. It's not meant to be resolved to a network location, although some implementations do try and resolve it.

    Second thing, validation itself doesn't resolve any references of a property value, there are packages you can use to resolve refs, but that's not in scope of the JSON Schema specification or the validation mechanism. You can use pattern to create a regex of a specific path uri pattern, but that's the extent of the validation.