Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | 262x 6x 9x 5x 5x 2x 3x | import { ValidationErrorItem } from '../errors/errors.ts';
import type { StandardSchemaV1 } from './standard-schema.ts';
import { StandardSchemaValidator } from './standard-schema-validator.ts';
import type { ValidationTarget } from './validation-target.ts';
/** Turns the issues of a Standard Schema into validation error items. */
export class SchemaItems {
readonly #schemas = new StandardSchemaValidator();
/**
* @param test - Value to check.
* @returns Whether `test` is a Standard Schema.
*/
isSchema(test: unknown): test is StandardSchemaV1 {
return this.#schemas.isSchema(test);
}
/**
* Validates a value against a schema.
*
* @param target - Instance being validated.
* @param schema - Standard Schema.
* @param value - Value to validate.
* @param path - Attribute the schema belongs to; `undefined` for a model schema.
* @returns One item per schema issue.
*/
async items(
target: ValidationTarget,
schema: StandardSchemaV1,
value: unknown,
path: string | undefined,
): Promise<readonly ValidationErrorItem[]> {
return (await this.#schemas.issues(schema, value)).map(
(issue) =>
new ValidationErrorItem(
issue.message,
'Validation error',
SchemaItems.#issuePath(path, issue.path),
value,
target.instance,
'schema',
),
);
}
static #issuePath(path: string | undefined, issuePath: string | undefined): string | null {
if (path === undefined) {
return issuePath ?? null;
}
return issuePath === undefined ? path : `${path}.${issuePath}`;
}
}
|