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