Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs

name: Build Test CI
name: CI

on:
push:
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<a href="https://www.npmjs.com/package/zenstack">
<img src="https://img.shields.io/npm/v/zenstack">
</a>
<img src="https://github.com/zenstackhq/zenstack/actions/workflows/build-test.yml/badge.svg">
<a href="https://twitter.com/zenstackhq">
<img src="https://img.shields.io/twitter/url?style=social&url=https%3A%2F%2Fgithub.com%2Fzenstackhq%2Fzenstack">
</a>
Expand Down
72 changes: 69 additions & 3 deletions docs/get-started/learning-the-zmodel-language.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,11 @@ model User {

```

This document serves as a quick overview for starting with the ZModel language. For more thorough explanations about data modeling, please check out [Prisma's schema references](https://www.prisma.io/docs/reference/api-reference/prisma-schema-reference).

## Access policies

Access policies use `@@allow` and `@@deny` rules to specify the eligibility of an operation over a model entity. The signatures of the attributes are:
Access policies express authorization logic in a declarative way. They use `@@allow` and `@@deny` rules to specify the eligibility of an operation over a model entity. The signatures of the attributes are:

```prisma
@@allow(operation, condition)
Expand Down Expand Up @@ -374,6 +376,70 @@ In this example, `user` refers to `user` field of `Membership` model because `sp

Please check out the [Collaborative Todo](../../samples/todo) for a complete example on using access policies.

## Summary
## Field constraints

This document serves as a quick overview for starting with the ZModel language. For more thorough explanations about data modeling, please check out [Prisma's schema references](https://www.prisma.io/docs/reference/api-reference/prisma-schema-reference).
Field constraints are used for attaching constraints to field values. Unlike access policies, field constraints only apply on individual fields, and are only checked for 'create' and 'update' operations.

Internally ZenStack uses [zod](https://github.com/colinhacks/zod) for validation. The checks are run in both the server-side CURD services and the clent-side React hooks. For the server side, upon validation error, HTTP 400 is returned with a body containing a `message` field for details. For the client side, a `ValidationError` is thrown.

The following attributes can be used to attach field constraints:

### String:

- `@length(_ min: Int?, _ max: Int?)`

Validates length of a string field.

- `@startsWith(_ text: String)`

Validates a string field value starts with the given text.

- `@endsWith(_ text: String)`

Validates a string field value ends with the given text.

- `@email()`

Validates a string field value is a valid email address.

- `@url()`

Validates a string field value is a valid url.

- `@datetime()`

Validates a string field value is a valid ISO datetime.

- `@regex(_ regex: String)`

Validates a string field value matches a regex.

### Number:

- `@gt(_ value: Int)`

Validates a number field is greater than the given value.

- `@gte(_ value: Int)`

Validates a number field is greater than or equal to the given value.

- `@lt(_ value: Int)`

Validates a number field is less than the given value.

- `@lte(_ value: Int)`

Validates a number field is less than or equal to the given value.

### Sample usage

```prisma
model User {
id String @id
handle String @regex("^[0-9a-zA-Z]{4,16}$")
email String @email @endsWith("@myorg.com")
profileImage String? @url
age Int @gt(0)
}
```
2 changes: 1 addition & 1 deletion docs/get-started/next-js.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Here we demonstrate the process with a simple Blog starter using [Next-Auth](htt
2. Create a new Next.js project from the ZenStack starter

```bash
npx create-next-app [project name] --use-npm -e https://github.com/zenstackhq/nextjs-auth-starter
npx create-next-app --use-npm -e https://github.com/zenstackhq/nextjs-auth-starter [project name]

cd [project name]
```
Expand Down