@zenstackhq/plugin-policy 3.10.1 → 3.10.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -320,7 +320,7 @@ var ExpressionTransformer = class {
320
320
  const leftFieldDef = this.getFieldDefFromFieldRef(normalizedLeft, context);
321
321
  const rightFieldDef = this.getFieldDefFromFieldRef(normalizedRight, context);
322
322
  const sqlOp = op === "==" ? "=" : op;
323
- const asText = this.isBeforeEnumField(normalizedLeft, context) || this.isBeforeEnumField(normalizedRight, context);
323
+ const asText = this.isBeforeEnumField(normalizedLeft, context) || this.isBeforeEnumField(normalizedRight, context) || this.isCreateInputEnumComparison(normalizedLeft, normalizedRight, context);
324
324
  const operand = (node) => asText ? this.dialect.castText(new kysely.ExpressionWrapper(node)) : new kysely.ExpressionWrapper(node);
325
325
  return this.dialect.buildComparison(operand(left), leftFieldDef, sqlOp, operand(right), rightFieldDef).toOperationNode();
326
326
  }
@@ -797,6 +797,50 @@ var ExpressionTransformer = class {
797
797
  const fieldDef = _zenstackhq_orm.QueryUtils.getField(this.schema, context.thisType, expr.members[0]);
798
798
  return !!fieldDef && _zenstackhq_orm.QueryUtils.isEnum(this.schema, fieldDef.type);
799
799
  }
800
+ /**
801
+ * Whether a comparison in a create rule needs both operands compared as text.
802
+ *
803
+ * Create rules are evaluated before the insert, against a `VALUES` table built from the input
804
+ * (see `enforcePreCreatePolicyForOne`). That table carries enum fields as `text`, while stored
805
+ * columns have the native enum type, and PostgreSQL has no `text = "Enum"` operator (42883).
806
+ *
807
+ * Returns true only when one operand is an enum field of the entity being created and the
808
+ * other is a stored field of the same enum, e.g. for `model Grant { permission Permission }`:
809
+ * - `permission == anchor.defaultPermission` (bare top-level field vs. related row)
810
+ * - `anchor.approvals?[permission == this.permission]` (stored row vs. `this`)
811
+ *
812
+ * Not for literals (`permission == EDIT`): the name mapper still translates `@map`-ed literals.
813
+ * Not for enums with `@map`-ed values: the input holds names (`ACTIVE`) but stored columns
814
+ * hold mapped values (`active`), so a text comparison would be wrong (#2867).
815
+ */
816
+ isCreateInputEnumComparison(left, right, context) {
817
+ if (context.operation !== "create") return false;
818
+ const leftInput = this.getCreateInputEnumField(left, context);
819
+ const rightInput = this.getCreateInputEnumField(right, context);
820
+ if (!leftInput === !rightInput) return false;
821
+ const inputDef = leftInput ?? rightInput;
822
+ return this.getFieldDefFromFieldRef(leftInput ? right : left, context)?.type === inputDef.type && !this.hasMappedEnumValues(inputDef.type);
823
+ }
824
+ /**
825
+ * If the expression reads an enum field of the entity being created (i.e. a column of the input
826
+ * `VALUES` table), returns that field's definition; otherwise `undefined`.
827
+ *
828
+ * - `this.field` always refers to the entity the rule belongs to. A longer path such as
829
+ * `this.anchor.defaultPermission` goes through a relation and reads a stored row.
830
+ * - a bare `field` refers to the entity being created only at the top level of the rule. Inside
831
+ * a collection predicate (`relation?[field == ...]`) it refers to the related stored row.
832
+ */
833
+ getCreateInputEnumField(expr, context) {
834
+ let field;
835
+ if (_zenstackhq_orm_schema.ExpressionUtils.isMember(expr) && _zenstackhq_orm_schema.ExpressionUtils.isThis(expr.receiver) && expr.members.length === 1) field = expr.members[0];
836
+ else if (_zenstackhq_orm_schema.ExpressionUtils.isField(expr) && context.modelOrType === context.thisType && context.alias === context.thisAlias) field = expr.field;
837
+ const fieldDef = field ? _zenstackhq_orm.QueryUtils.getField(this.schema, context.thisType, field) : void 0;
838
+ return fieldDef && _zenstackhq_orm.QueryUtils.isEnum(this.schema, fieldDef.type) ? fieldDef : void 0;
839
+ }
840
+ hasMappedEnumValues(enumType) {
841
+ const enumDef = _zenstackhq_orm.QueryUtils.getEnum(this.schema, enumType);
842
+ return Object.values(enumDef?.fields ?? {}).some((f) => f.attributes?.some((attr) => attr.name === "@map"));
843
+ }
800
844
  getFieldDefFromFieldRef(expr, context) {
801
845
  return this.resolveFieldRef(expr, context)?.fieldDef;
802
846
  }