Repository: directus/directus
File: api/src/permissions/modules/validate-access/lib/validate-item-access.ts (TypeScript)

Code before:
import { processAst } from '../../process-ast/process-ast.js';

export interface ValidateItemAccessOptions {
	accountability: Accountability;
	action: PermissionsAction;
	collection: string;
	primaryKeys: PrimaryKey[];
	fields?: string[];
	returnAllowedRootFields?: boolean;
}

export interface ValidateItemAccessOptionsWithFields extends ValidateItemAccessOptions {
	returnAllowedRootFields: true;
}

export interface ValidateItemAccessResult {
	accessAllowed: boolean;
	allowedRootFields?: string[];
}

export interface ValidateItemAccessResultWithFields extends ValidateItemAccessResult {
	allowedRootFields: string[];
}

export async function validateItemAccess(
	options: ValidateItemAccessOptionsWithFields,
	context: Context,
): Promise<ValidateItemAccessResultWithFields>;

export async function validateItemAccess(
	options: ValidateItemAccessOptions,
	context: Context,
): Promise<ValidateItemAccessResult>;

export async function validateItemAccess(
	options: ValidateItemAccessOptions | ValidateItemAccessOptionsWithFields,
	context: Context,
): Promise<ValidateItemAccessResult | ValidateItemAccessResultWithFields> {
	const primaryKeyField = context.schema.collections[options.collection]?.primary;

	if (!primaryKeyField) {
		throw new Error(`Cannot find primary key for collection "${options.collection}"`);
	}

	// When we're looking up access to specific items, we have to read them from the database to
	// make sure you are allowed to access them.

	const ast: AST = {
		type: 'root',
		name: options.collection,
		query: { limit: options.primaryKeys.length },

Review comment (suggestion):
```suggestion
	options: ValidateItemAccessOptionsWithRootFields,
```

Code after:
import { processAst } from '../../process-ast/process-ast.js';

export interface ValidateItemAccessOptions {
	accountability: Accountability;
	action: PermissionsAction;
	collection: string;
	primaryKeys: PrimaryKey[];
	fields?: string[];
	returnAllowedRootFields?: boolean;
}

export interface ValidateItemAccessOptionsWithRootFields extends ValidateItemAccessOptions {
	returnAllowedRootFields: true;
}

export interface ValidateItemAccessResult {
	accessAllowed: boolean;
	allowedRootFields?: string[];
}

export interface ValidateItemAccessResultWithRootFields extends ValidateItemAccessResult {
	allowedRootFields: string[];
}

export async function validateItemAccess(
	options: ValidateItemAccessOptionsWithRootFields,
	context: Context,
): Promise<ValidateItemAccessResultWithRootFields>;

export async function validateItemAccess(
	options: ValidateItemAccessOptions,
	context: Context,
): Promise<ValidateItemAccessResult>;

export async function validateItemAccess(
	options: ValidateItemAccessOptions | ValidateItemAccessOptionsWithRootFields,
	context: Context,
): Promise<ValidateItemAccessResult | ValidateItemAccessResultWithRootFields> {
	const primaryKeyField = context.schema.collections[options.collection]?.primary;

	if (!primaryKeyField) {
		throw new Error(`Cannot find primary key for collection "${options.collection}"`);
	}

	// When we're looking up access to specific items, we have to read them from the database to
	// make sure you are allowed to access them.

	const ast: AST = {
		type: 'root',
		name: options.collection,
		query: { limit: options.primaryKeys.length },