fixed schema validation including test

master
Martin Scheidt 2022-12-19 21:41:30 +01:00
parent 2ffdcb1fb9
commit b276270ea6
4 changed files with 53 additions and 49 deletions

View File

@ -9,6 +9,9 @@ Categories: Added, Changed, Deprecated, Removed, Fixed, and Security.
## [Unreleased] ## [Unreleased]
### Fixed
* extended documentation strings for some functions
* loading errors with illstructured path file
## Version [1.0.2] 2022-09-01 ## Version [1.0.2] 2022-09-01

View File

@ -64,9 +64,7 @@ function Settings(
settings = YAML.load(open(file))["settings"] settings = YAML.load(open(file))["settings"]
## validate the loaded file ## validate the loaded file
try if !isvalid(schema, settings)
validate(schema, settings)
catch err
println("Could not load settings file '$file'.\n Format is not recognized - using default as fall back.") println("Could not load settings file '$file'.\n Format is not recognized - using default as fall back.")
settings = Dict() settings = Dict()
end end
@ -112,17 +110,8 @@ function Path(file, type = :YAML)
## load from file ## load from file
if type == :YAML if type == :YAML
data = YAML.load(open(file)) ## error messages
if data["schema"] != "https://railtoolkit.org/schema/running-path.json" format_error = "\n\tCould not parse file '$file'.\n\tNot a valide railtoolkit/schema format.\n\tCurrently supported version: 2022.05\n\tFor the format see: https://github.com/railtoolkit/schema"
error("Could not load path file '$file'.\n
YAML format is not recognized.
Currently supported: railtoolkit/schema/running-path (2022.05)")
end
if data["schema_version"] != "2022.05"
error("Could not load path file '$file'.\n
YAML format is not recognized.
Currently supported: railtoolkit/schema/running-path (2022.05)")
end
## JSON schema for YAML-file validation ## JSON schema for YAML-file validation
railtoolkit_schema = Schema("""{ railtoolkit_schema = Schema("""{
@ -206,18 +195,13 @@ function Path(file, type = :YAML)
} }
}""") }""")
paths = data["paths"] data = YAML.load(open(file))
try data["schema"] == "https://railtoolkit.org/schema/running-path.json" ? nothing : throw(DomainError(data["schema"],format_error))
validate(railtoolkit_schema, paths) data["schema_version"] == "2022.05" ? nothing : throw(DomainError(data["schema_version"],format_error))
catch err isvalid(railtoolkit_schema, data["paths"]) ? nothing : throw(DomainError(data["paths"],format_error))
error("Could not load path file '$file'.\n
YAML format is not recognized. length(data["paths"]) > 1 ? println("WARNING: the loaded file contains more than one path. Using only the first!") : nothing
Currently supported: railtoolkit/schema/running-path (2022.05)") path = data["paths"][1]
end
if length(paths) > 1
println("WARNING: the loaded file contains more than one path. Using only the first!")
end
path = paths[1]
## set the variables in "path" ## set the variables in "path"
# required # required
@ -230,7 +214,7 @@ function Path(file, type = :YAML)
haskey(path, "points_of_interest") ? tmp_points = path["points_of_interest"] : nothing haskey(path, "points_of_interest") ? tmp_points = path["points_of_interest"] : nothing
else else
error("Unknown file type '$type'") throw(DomainError("Unknown file type '$type'"))
end #if type end #if type
## process characteristic sections ## process characteristic sections
@ -313,17 +297,8 @@ function Train(file, type = :YAML)
## load from file ## load from file
if type == :YAML if type == :YAML
data = YAML.load(open(file)) ## error messages
if data["schema"] != "https://railtoolkit.org/schema/rolling-stock.json" format_error = "\n\tCould not parse file '$file'.\n\tNot a valide railtoolkit/schema format.\n\tCurrently supported version: 2022.05\n\tFor the format see: https://github.com/railtoolkit/schema"
error("Could not load path file '$file'.\n
YAML format is not recognized.
Currently supported: railtoolkit/schema/rolling-stock (2022.05)")
end
if data["schema_version"] != "2022.05"
error("Could not load path file '$file'.\n
YAML format is not recognized.
Currently supported: railtoolkit/schema/rolling-stock (2022.05)")
end
## JSON schema for YAML-file validation ## JSON schema for YAML-file validation
railtoolkit_schema = Schema("""{ railtoolkit_schema = Schema("""{
@ -474,21 +449,19 @@ function Train(file, type = :YAML)
} }
}""") }""")
try ## validation
validate(railtoolkit_schema, data) data = YAML.load(open(file))
catch err data["schema"] == "https://railtoolkit.org/schema/rolling-stock.json" ? nothing : throw(DomainError(data["schema"],format_error))
error("Could not load path file '$file'.\n data["schema_version"] == "2022.05" ? nothing : throw(DomainError(data["schema_version"],format_error))
YAML format is not recognized. isvalid(railtoolkit_schema, data) ? nothing : throw(DomainError(data,format_error))
Currently supported: railtoolkit/schema/rolling-stock (2022.05)")
end
else else
error("Unknown file type '$type'") throw(DomainError("Unknown file type '$type'"))
end #if type end #if type
trains = data["trains"] trains = data["trains"]
Base.length(trains) > 1 ? println("WARNING: the loaded file contains more than one train. Using only the first!") : nothing Base.length(trains) > 1 ? println("WARNING: the loaded file contains more than one train. Using only the first!") : nothing
Base.length(trains) == 0 ? error("No train present in file '$file'") : nothing Base.length(trains) == 0 ? throw(DomainError("No train present in file '$file'")) : nothing
train = trains[1] train = trains[1]
used_vehicles = unique(train["formation"]) used_vehicles = unique(train["formation"])
@ -499,7 +472,7 @@ function Train(file, type = :YAML)
## test if all vehicles of the formation are avilable ## test if all vehicles of the formation are avilable
for vehicle in used_vehicles for vehicle in used_vehicles
vehicle included_vehicles ? error("'$vehicle' is not present in '$file'") : nothing vehicle included_vehicles ? throw(DomainError("'$vehicle' is not present in '$file'")) : nothing
end end
## gather the count of vehicles and usage in the formation ## gather the count of vehicles and usage in the formation
@ -544,7 +517,7 @@ function Train(file, type = :YAML)
end end
Base.length(loco) > 1 ? println("WARNING: the loaded file contains more than one traction unit or multiple unit. Using only the first!") : nothing Base.length(loco) > 1 ? println("WARNING: the loaded file contains more than one traction unit or multiple unit. Using only the first!") : nothing
loco[1].n > 1 ? println("WARNING: the loaded file contains more than one traction unit or multiple unit. Using only one!") : nothing loco[1].n > 1 ? println("WARNING: the loaded file contains more than one traction unit or multiple unit. Using only one!") : nothing
Base.length(loco) == 0 ? error("No traction unit or multiple unit present in file '$file'") : nothing Base.length(loco) == 0 ? throw(DomainError("No traction unit or multiple unit present in file '$file'")) : nothing
loco = loco[1].data loco = loco[1].data
cars = vehicles cars = vehicles

View File

@ -0,0 +1,27 @@
%YAML 1.2
---
schema: https://railtoolkit.org/schema/running-path.json
schema_version: "2022.05"
paths:
id: broken
UUID: 53778d7d-ee4f-4edb-9d17-5341dbd517f6
name: "broken test"
points_of_interest:
-
- 850.00
- view_point
- front
characteristic_sections:
-
- 0.00
- 200.0
- 0.00
-
- 855.84
- 200.0
- -0.44
-
- 968.06
- 200.0
- -0.44

View File

@ -36,6 +36,7 @@ settings = Dict()
@test typeof(first(paths)[2]) == Path @test typeof(first(paths)[2]) == Path
@test typeof(first(settings)[2]) == Settings @test typeof(first(settings)[2]) == Settings
@test_throws DomainError Path("data/paths/broken.yaml")
end end
println("====================") println("====================")