Home / File/ validators.ts — astro Source File

validators.ts — astro Source File

Architecture documentation for validators.ts, a typescript file in the astro codebase. 1 imports, 0 dependents.

File typescript CoreAstro RenderingEngine 1 imports 8 functions

Entity Profile

Dependency Diagram

graph LR
  507d4b59_27b6_fc34_cbc8_02ec1d1596e5["validators.ts"]
  3a338670_ef65_2b57_cc61_29c1c222b236["../../env/schema.js"]
  507d4b59_27b6_fc34_cbc8_02ec1d1596e5 --> 3a338670_ef65_2b57_cc61_29c1c222b236
  style 507d4b59_27b6_fc34_cbc8_02ec1d1596e5 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import type { EnumSchema, EnvFieldType, NumberSchema, StringSchema } from './schema.js';

export type ValidationResultValue = EnvFieldType['default'];
export type ValidationResultErrors = ['missing'] | ['type'] | Array<string>;
interface ValidationResultValid {
	ok: true;
	value: ValidationResultValue;
}
export interface ValidationResultInvalid {
	ok: false;
	errors: ValidationResultErrors;
}
type ValidationResult = ValidationResultValid | ValidationResultInvalid;

export function getEnvFieldType(options: EnvFieldType) {
	const optional = options.optional ? (options.default !== undefined ? false : true) : false;

	let type: string;
	if (options.type === 'enum') {
		type = options.values.map((v) => `'${v}'`).join(' | ');
	} else {
		type = options.type;
	}

	return `${type}${optional ? ' | undefined' : ''}`;
}

type ValueValidator = (input: string | undefined) => ValidationResult;

const stringValidator =
	({ max, min, length, url, includes, startsWith, endsWith }: StringSchema): ValueValidator =>
	(input) => {
		if (typeof input !== 'string') {
			return {
				ok: false,
				errors: ['type'],
			};
		}
		const errors: Array<string> = [];

		if (max !== undefined && !(input.length <= max)) {
			errors.push('max');
		}
		if (min !== undefined && !(input.length >= min)) {
			errors.push('min');
		}
		if (length !== undefined && !(input.length === length)) {
			errors.push('length');
		}
		if (url !== undefined && !URL.canParse(input)) {
			errors.push('url');
		}
		if (includes !== undefined && !input.includes(includes)) {
			errors.push('includes');
		}
		if (startsWith !== undefined && !input.startsWith(startsWith)) {
			errors.push('startsWith');
		}
		if (endsWith !== undefined && !input.endsWith(endsWith)) {
			errors.push('endsWith');
// ... (120 more lines)

Domain

Subdomains

Dependencies

  • ../../env/schema.js

Frequently Asked Questions

What does validators.ts do?
validators.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, RenderingEngine subdomain.
What functions are defined in validators.ts?
validators.ts defines 8 function(s): ValidationResult, booleanValidator, enumValidator, getEnvFieldType, numberValidator, selectValidator, stringValidator, validateEnvVariable.
What does validators.ts depend on?
validators.ts imports 1 module(s): ../../env/schema.js.
Where is validators.ts in the architecture?
validators.ts is located at packages/astro/src/env/validators.ts (domain: CoreAstro, subdomain: RenderingEngine, directory: packages/astro/src/env).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free