YASL API Reference
yasl
YASL - YAML Advanced Schema Language
YASL is an advanced schema language & validation tool for YAML data. YASL supports definition and validation of data structures with primitives, enumerations, and composition of defined types. YASL also supports references between types and properties, enabling complex data models.
check_paths(paths, model_name=None, disable_log=False, quiet_log=False, verbose_log=False, output='text', log_stream=sys.stdout)
Check mixed YASL schemas and YAML data from a list of paths.
This function recursively scans the provided paths for YASL schema files (.yasl) and YAML data files (.yaml, .yml). It employs a heuristic to distinguish between schema and data files regardless of extension: if a file parses strictly as a valid YASL schema (YaslRoot), it is treated as such; otherwise, it is treated as data to be validated.
Process:
1. Scan paths for all candidate files.
2. Classify each file as Schema or Data.
3. Compile all identified Schemas into the YaslRegistry.
4. Validate all identified Data files against the registered schemas.
- If model_name is provided, validate against that specific model.
- Otherwise, auto-detect the schema based on root keys.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
paths
|
list[str]
|
List of file or directory paths to scan. |
required |
model_name
|
str | None
|
Optional specific schema type name to enforce for validation. |
None
|
disable_log
|
bool
|
If True, disables all logging output. |
False
|
quiet_log
|
bool
|
If True, suppresses all output except for errors. |
False
|
verbose_log
|
bool
|
If True, enables verbose logging output. |
False
|
output
|
str
|
Output format for logs ('text', 'json', 'yaml'). Default 'text'. |
'text'
|
log_stream
|
StringIO | TextIO
|
Stream to write logs to. Default stdout. |
stdout
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if all schemas are valid AND all data files validate successfully. False if any schema fails to compile or any data file fails validation. |
Source code in src/yasl/core.py
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 | |
check_schema(yasl_schema, disable_log=False, quiet_log=False, verbose_log=False, output='text', log_stream=sys.stdout)
Check the validity of a YASL schema file or directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
yasl_schema
|
str
|
Path to the YASL schema file or directory. |
required |
disable_log
|
bool
|
If True, disables all logging output. |
False
|
quiet_log
|
bool
|
If True, suppresses all output except for errors. |
False
|
verbose_log
|
bool
|
If True, enables verbose logging output. |
False
|
output
|
str
|
Output format for logs. Options are 'text', 'json', or 'yaml'. Default is 'text'. |
'text'
|
log_stream
|
StringIO
|
Stream to which logs will be written. Default is sys.stdout. |
stdout
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the schema is valid, False otherwise. |
Source code in src/yasl/core.py
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 | |
get_yasl_registry()
Get the singleton YaslRegistry instance.
Source code in src/yasl/cache.py
342 343 344 | |
load_data(yaml_data, schema_name, schema_namespace=None)
Validate a dictionary of data against a specific registered YASL schema.
This function retrieves the Pydantic model corresponding to the given schema name and namespace from the YaslRegistry, and then attempts to validate the provided data against it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
yaml_data
|
dict[str, Any]
|
The raw dictionary containing the YAML data to validate. |
required |
schema_name
|
str
|
The name of the schema type to validate against. |
required |
schema_namespace
|
str | None
|
The namespace where the schema is defined. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
An instance of the validated Pydantic model if successful, |
Any
|
or None if validation fails or the schema cannot be found. |
Note
The function catches ValidationError and SyntaxError, logs the details, and returns None.
Source code in src/yasl/core.py
1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 | |
load_data_files(path, model_name=None)
Load and validate YAML data from a file against YASL schemas.
This function reads a YAML file (which may contain multiple documents) and attempts to validate each document against a registered YASL schema.
If model_name is provided, validation is attempted against that specific schema.
If model_name is None, the function attempts to auto-detect the appropriate schema
by matching the root keys of the YAML data against the fields of registered types.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
The file path to the YAML data file. |
required |
model_name
|
str | None
|
The name of the schema to validate against. If None, schema auto-detection is performed. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
A list of validated Pydantic models (one for each document in the YAML file) |
Any
|
if successful, or None if validation fails or the file cannot be read. |
Note
The function catches exceptions like FileNotFoundError, SyntaxError, YAMLError, and ValidationError, logging them as errors and returning None.
Source code in src/yasl/core.py
1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 | |
load_schema(data)
Load and validate a YASL schema from a dictionary and add the generated types to the registry.
This function parses a raw dictionary into a YaslRoot object, generating
any defined enumerations and Pydantic models in the process. Note that
schema imports are NOT supported when loading directly from a dictionary;
use load_schema_files if import resolution is required.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any]
|
The raw dictionary containing the YASL schema definition. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
YaslRoot |
YaslRoot
|
The validated and parsed YASL root object. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the schema defines imports (which are not supported in this mode), or if type generation fails (e.g. duplicate definitions, invalid references). |
ValidationError
|
If the input data does not match the expected YASL structure. |
Source code in src/yasl/core.py
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 | |
load_schema_files(path)
Load and validate YASL schema(s) from a file.
This function reads a YAML file containing one or more YASL schema definitions. It recursively resolves any imports specified in the schemas. For each valid schema, it generates the corresponding Python Enums and Pydantic models and registers them in the YaslRegistry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
The file path to the YASL schema file. |
required |
Returns:
| Type | Description |
|---|---|
list[YaslRoot] | None
|
list[YaslRoot] | None: A list of validated YaslRoot objects if successful, |
list[YaslRoot] | None
|
or None if validation fails or the file cannot be read. |
Note
The function catches most exceptions (FileNotFoundError, YAMLError, ValidationError) and logs them as errors, returning None.
Source code in src/yasl/core.py
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 | |
yasl_eval(yasl_schema, yaml_data, model_name=None, disable_log=False, quiet_log=False, verbose_log=False, output='text', log_stream=sys.stdout)
Evaluate YAML data against a YASL schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
yasl_schema
|
str
|
Path to the YASL schema file or directory. |
required |
yaml_data
|
str
|
Path to the YAML data file or directory. |
required |
model_name
|
str
|
Specific model name to use for validation. If not provided, the model will be auto-detected. |
None
|
disable_log
|
bool
|
If True, disables all logging output. |
False
|
quiet_log
|
bool
|
If True, suppresses all output except for errors. |
False
|
verbose_log
|
bool
|
If True, enables verbose logging output. |
False
|
output
|
str
|
Output format for logs. Options are 'text', 'json', or 'yaml'. Default is 'text'. |
'text'
|
log_stream
|
StringIO
|
Stream to which logs will be written. Default is sys.stdout. |
stdout
|
Returns:
| Type | Description |
|---|---|
list[BaseModel] | None
|
Optional[List[BaseModel]]: List of validated Pydantic models if validation is successful, None otherwise. |
Source code in src/yasl/core.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | |
yasl_version()
Get the version of the YASL package.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The version string defined in pyproject.toml, or an error message if the file cannot be read. |
Source code in src/yasl/core.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | |