Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check with jsonschema if path is valid

I am trying to write a json schema including the feature to check if a path is valid and exist.

For example I want to validate this json:

{
  "paths": ["/path/to_check", "../path/not/valid", "../../path/exists"]
}

My current schema is:

{
  "type": "object",
  "properties": {
     "paths": {
       "type": "array",
       "items": {
         "type": "string"
       }
     }
  }
}

Is there a way to indicate that items must contain valid/existing paths?

like image 548
Alessandro Di Bella Avatar asked Sep 06 '25 03:09

Alessandro Di Bella


2 Answers

You can use regex, but there's no way to determine if a path is a real path according to a file system. JSON Schema works with JSON data... that's all, nothing more. It has no notion of a file system.

like image 172
Relequestual Avatar answered Sep 07 '25 22:09

Relequestual


I've been googling for the same thing and was coming to the same conclusion that @Relequestual posted (who is the authority on such things).

What may be of interest is that the pydantic library extends their JSON Schema with a bunch of extensions for complex string sub-types, including file-path, directory-path and path (and many, many more).

This could be useful either directly, or to adopt as a quasi-standard for a custom implementation.

like image 20
ewels Avatar answered Sep 07 '25 22:09

ewels