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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ import { UserSchema } from '@seamless-auth/types';
const user = UserSchema.parse(data);
```

User role schemas accept plain roles such as `admin` and colon-separated scoped roles such as
`admin:read` and `admin:write`. Whitespace, underscores, slashes, and backslashes are rejected.

### Infer types

```ts
Expand Down
27 changes: 27 additions & 0 deletions src/schemas/user/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ describe('UserSchema', () => {
).toThrow();
});

it('allows scoped role format', () => {
expect(() =>
UserSchema.parse({
...baseUser,
roles: ['admin:read', 'admin:write'],
}),
).not.toThrow();
});

it('allows nullable lastLogin', () => {
expect(() =>
UserSchema.parse({
Expand Down Expand Up @@ -84,6 +93,16 @@ describe('CreateUserSchema', () => {
).not.toThrow();
});

it('parses scoped roles', () => {
const parsed = CreateUserSchema.parse({
email: 'test@example.com',
phone: '1234567890',
roles: ['admin:read'],
});

expect(parsed.roles).toEqual(['admin:read']);
});

it('fails if roles are empty', () => {
expect(() =>
CreateUserSchema.parse({
Expand Down Expand Up @@ -148,6 +167,14 @@ describe('UpdateUserSchema', () => {
).toThrow();
});

it('parses scoped role updates', () => {
const parsed = UpdateUserSchema.parse({
roles: ['admin:write'],
});

expect(parsed.roles).toEqual(['admin:write']);
});

it('fails on unknown fields due to strict()', () => {
expect(() =>
UpdateUserSchema.parse({
Expand Down
5 changes: 4 additions & 1 deletion src/schemas/user/schema.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { z } from 'zod';
import { IsoDate } from '../../shared.js';

export const RoleSchema = z.string().regex(/^(?!.*[_/\\\s])[A-Za-z0-9-]{1,31}$/);
export const RoleSchema = z
.string()
.trim()
.regex(/^(?!.*[_/\\\s])(?=.{1,80}$)[A-Za-z0-9-]+(?::[A-Za-z0-9-]+)*$/);

export const UserSchema = z.object({
id: z.uuid(),
Expand Down
Loading