prisma-zod-generator
Version:
Prisma 2+ generator to emit Zod schemas from your Prisma schema
115 lines (114 loc) • 3.71 kB
TypeScript
/**
* PZG Pro - Comment Parser
*
* Parses policy and PII annotations from Prisma schema comments
*/
export interface PolicyRule {
type: 'read' | 'write' | 'deny' | 'update' | 'delete' | 'create';
subtype?: 'where' | 'fields' | 'values';
condition: string;
field?: string;
fields?: string[];
modelName?: string;
operator?: 'in' | 'not_in' | 'equals' | 'not_equals' | 'contains' | 'starts_with' | 'ends_with' | '==' | '!=';
contextVariable?: string;
}
export interface PIIRule {
type: 'pii';
dataType: 'email' | 'phone' | 'ssn' | 'credit_card' | 'custom';
redactLogs?: boolean;
maskType?: 'partial' | 'full' | 'hash';
field?: string;
modelName?: string;
}
export interface TenantRule {
type: 'tenant';
field: string;
fields?: string[];
tenantType?: 'string' | 'number' | 'uuid';
tenantTypes?: ('string' | 'number' | 'uuid')[];
required?: boolean;
modelName?: string;
hierarchy?: {
parent?: string;
children?: string[];
depth?: number;
};
sharding?: {
strategy: 'hash' | 'range' | 'directory';
shardCount?: number;
shardField?: string;
};
inheritance?: {
inheritsFrom?: string;
cascadeDelete?: boolean;
inheritanceType: 'single-table' | 'joined-table' | 'table-per-class';
};
validation?: {
format?: 'uuid' | 'slug' | 'domain' | 'custom';
pattern?: string;
length?: {
min?: number;
max?: number;
};
enum?: string[];
};
performance?: {
indexStrategy: 'btree' | 'hash' | 'gin' | 'gist';
partitioning?: 'range' | 'hash' | 'list';
caching?: {
ttl: number;
strategy: 'redis' | 'memory';
};
};
}
export interface ModelPolicies {
modelName: string;
policies: PolicyRule[];
piiRules: PIIRule[];
tenantRules: TenantRule[];
}
/**
* Parse a Prisma schema file and extract all policy/PII rules
*/
export declare function parseSchemaComments(schemaContent: string): ModelPolicies[];
/**
* Get tenant fields for a specific model
*/
export declare function getTenantFields(modelPolicies: ModelPolicies[]): Record<string, TenantRule[]>;
/**
* Detect tenant fields automatically from field names (fallback)
*/
export declare function detectTenantFieldsFromSchema(schemaContent: string): Record<string, TenantRule[]>;
/**
* Get all fields that have read restrictions for a model
*/
export declare function getReadRestrictedFields(modelPolicies: ModelPolicies): string[];
/**
* Get all fields that have write restrictions for a model
*/
export declare function getWriteRestrictedFields(modelPolicies: ModelPolicies): string[];
/**
* Get all PII fields for a model
*/
export declare function getPIIFields(modelPolicies: ModelPolicies): PIIRule[];
/**
* Check if a field has a specific policy condition
*/
export declare function hasPolicy(modelPolicies: ModelPolicies, field: string, type: 'read' | 'write' | 'deny' | 'update' | 'delete' | 'create'): PolicyRule | undefined;
/**
* Get tenant isolation policies (where clauses)
*/
export declare function getTenantPolicies(modelPolicies: ModelPolicies): PolicyRule[];
/**
* Get field-level restriction policies
*/
export declare function getFieldRestrictions(modelPolicies: ModelPolicies, operation: 'read' | 'update' | 'create'): PolicyRule[];
/**
* Get value validation policies
*/
export declare function getValuePolicies(modelPolicies: ModelPolicies): PolicyRule[];
/**
* Generate where clause for tenant isolation
*/
export declare function generateTenantWhereClause(policies: PolicyRule[], contextVar?: string): string;