prisma-zod-generator
Version:
Prisma 2+ generator to emit Zod schemas from your Prisma schema
101 lines (100 loc) • 2.8 kB
TypeScript
/**
* PZG Pro - Policies Generator (DMMF-based)
*
* Generate field-level security and PII redaction from DMMF data with comment annotations
*/
import { DMMF } from '@prisma/generator-helper';
import { ProFeatureBase } from '../../core/ProFeatureBase';
import { ProGeneratorContext } from '../../core/ProGeneratorContext';
export interface PolicyConfig {
outputPath?: string;
enableRedaction?: boolean;
enableRLS?: boolean;
roles?: string[];
}
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 ModelPolicies {
modelName: string;
model: DMMF.Model;
policies: PolicyRule[];
piiRules: PIIRule[];
}
export declare class PoliciesGenerator extends ProFeatureBase {
private config;
constructor(context: ProGeneratorContext, config?: PolicyConfig);
protected getFeatureName(): string;
protected generateFeature(): Promise<void>;
/**
* Extract policy and PII annotations from DMMF models
*/
private extractModelPolicies;
/**
* Extract policy rules from model and field documentation
*/
private extractModelPolicyRules;
/**
* Extract PII rules from model and field documentation
*/
private extractModelPIIRules;
/**
* Parse policy annotation text into PolicyRule
*/
private parsePolicyAnnotation;
/**
* Parse PII annotation text into PIIRule
*/
private parsePIIAnnotation;
/**
* Generate policy files for a model
*/
private generateModelPolicyFiles;
/**
* Generate safe CRUD module for a model
*/
private generateSafeCRUDModule;
/**
* Generate redaction middleware for a model
*/
private generateRedactionMiddleware;
/**
* Generate DTO schemas for a model
*/
private generateDTOSchemas;
/**
* Get Zod type for a DMMF field
*/
private getZodTypeForField;
/**
* Get Zod scalar type
*/
private getZodScalarType;
/**
* Generate PII exclusions for public schema
*/
private generatePIIExclusions;
/**
* Generate role-based exclusions
*/
private generateRoleBasedExclusions;
/**
* Generate index files
*/
private generateIndexFiles;
}