zenstack icon indicating copy to clipboard operation
zenstack copied to clipboard

[Feature Request] Separate files for model-level policies or validation

Open nwidynski opened this issue 1 year ago • 8 comments

Is your feature request related to a problem? Please describe. When developing complex access policies or validation a single zmodel can quickly get very chaotic.

Describe the solution you'd like A way to put model-level access policies and validation in a separate file or abstract model.

Describe alternatives you've considered We tried creating abstract models, which contain the model-level policies, but for Zenstack to recognize the fields we would have to extend a common base model, which it then complains is extended from multiple times.

abstract model CounterSchema {
  name String
  value Int
}

//! If we don't extend CounterSchema here, Zenstack doesn't recognize "value"
abstract model CounterCreate extends CounterSchema {
  @@allow("create", value >= 0)
}

//! If we don't extend CounterSchema here, Zenstack doesn't recognize "name"
abstract model CounterRead extends CounterSchema {
  @@allow("read", name == "It should be readable")
}

//! This fails compilation because CounterSchema is extended twice
model Counter extends CounterCreate, CounterRead {
  id String @id
}

nwidynski avatar Mar 04 '24 10:03 nwidynski

we would have to extend a common base model, which it then complains is extended from multiple times.

Would you please give us an example for this?

jiashengguo avatar Mar 04 '24 12:03 jiashengguo

Maybe something like "partial models" can mitigate this problem? Like:

model Post {
  id Int @id
  title String
}

model Post {
  @@allow(...)
}

The two Post models can reside in different zmodel files and are merged during compilation.

ymc9 avatar Mar 04 '24 17:03 ymc9

we would have to extend a common base model, which it then complains is extended from multiple times.

Would you please give us an example for this?

I updated the description.

nwidynski avatar Mar 04 '24 17:03 nwidynski

Maybe something like "partial models" can mitigate this problem? Like:

model Post {
  id Int @id
  title String
}

model Post {
  @@allow(...)
}

The two Post models can reside in different zmodel files and are merged during compilation.

Is this something that is working already? I'm asking because there is nothing in the docs about this.

nwidynski avatar Mar 05 '24 08:03 nwidynski

Maybe something like "partial models" can mitigate this problem? Like:

model Post {

id Int @id

title String

}

model Post {

@@allow(...)

}

The two Post models can reside in different zmodel files and are merged during compilation.

Is this something that is working already? I'm asking because there is nothing in the docs about this.

No, it's just a proposal😄. Sorry for causing confusion.

ymc9 avatar Mar 05 '24 15:03 ymc9

Maybe something like "partial models" can mitigate this problem? Like:

model Post {

id Int @id

title String

}

model Post {

@@Allow(...)

}

The two Post models can reside in different zmodel files and are merged during compilation.

Is this something that is working already? I'm asking because there is nothing in the docs about this.

No, it's just a proposal😄. Sorry for causing confusion.

Thought so 😄 . But yeah, a partial merge during compilation would be much appreciated.

nwidynski avatar Mar 06 '24 14:03 nwidynski

Got it. I've put it into V2 milestone and will continue prioritize.

ymc9 avatar Mar 08 '24 16:03 ymc9