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
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ export default class FunctionInvocationValidator implements AstValidator<Express
if (['uuid', 'ulid', 'cuid', 'nanoid'].includes(funcDecl.name)) {
const formatParamIdx = funcDecl.params.findIndex(param => param.name === 'format');
const formatArg = getLiteral<string>(expr.args[formatParamIdx]?.value);
if (formatArg && !formatArg.includes('%s')) {
if (
formatArg !== undefined &&
!/(?<!\\)%s/g.test(formatArg) // an unescaped %s must be present
) {
accept('error', 'argument must include "%s"', {
node: expr.args[formatParamIdx]!,
});
Expand Down
61 changes: 61 additions & 0 deletions packages/language/test/function-invocation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,70 @@ describe('Function Invocation Tests', () => {
}
`,
);

await loadSchema(
`
datasource db {
provider = 'sqlite'
url = 'file:./dev.db'
}

model User {
id String @id @default(uuid(7, '\\\\%s_%s'))
}
`,
);

await loadSchema(
`
datasource db {
provider = 'sqlite'
url = 'file:./dev.db'
}

model User {
id String @id @default(uuid(7, '%s_\\\\%s'))
}
`,
);
});

it('id functions should reject invalid format strings', async () => {
await loadSchemaWithError(`
datasource db {
provider = 'sqlite'
url = 'file:./dev.db'
}

model User {
id String @id @default(cuid(2, ''))
}
`, 'argument must include');

await loadSchemaWithError(
`
datasource db {
provider = 'sqlite'
url = 'file:./dev.db'
}

model User {
id String @id @default(uuid(4, '\\\\%s'))
}
`, 'argument must include');

await loadSchemaWithError(
`
datasource db {
provider = 'sqlite'
url = 'file:./dev.db'
}

model User {
id String @id @default(uuid(4, '\\\\%s\\\\%s'))
}
`, 'argument must include');

await loadSchemaWithError(
`
datasource db {
Expand Down
12 changes: 8 additions & 4 deletions tests/e2e/orm/client-api/generated-id-format-strings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,10 @@ describe('generated id format strings', () => {
const escapedSchema = `
model EscapedTest {
id Int @id
escaped String @default(uuid(4, "prefix_\\\\%s_suffix"))
consecutive String @default(uuid(4, "%s%s"))
mixedEscaped String @default(uuid(4, "\\\\%s_%s_end"))
mixedEscaped2 String @default(uuid(4, "%s_\\\\%s_end"))
mixedEscaped3 String @default(uuid(4, "\\\\%s_\\\\%s_%s"))
startWithPattern String @default(uuid(4, "%s_suffix"))
endWithPattern String @default(uuid(4, "prefix_%s"))
}
Expand All @@ -158,15 +159,18 @@ model EscapedTest {
},
});

// Escaped \%s should become literal %s in output
expect(record.escaped).toMatch(/^prefix_%s_suffix$/);

// Consecutive %s%s should both be replaced
expect(record.consecutive).toMatch(/^[0-9a-f-]{36}[0-9a-f-]{36}$/);

// Mixed: first \%s stays as %s, second %s is replaced
expect(record.mixedEscaped).toMatch(/^%s_[0-9a-f-]{36}_end$/);

// Mixed: first %s is replaced, second \%s stays as %s
expect(record.mixedEscaped2).toMatch(/^[0-9a-f-]{36}_%s_end$/);

// Mixed: first and second \%s stays as %s, third %s is replaced
expect(record.mixedEscaped3).toMatch(/^%s_%s_[0-9a-f-]{36}$/);

// Pattern at start
expect(record.startWithPattern).toMatch(/^[0-9a-f-]{36}_suffix$/);

Expand Down
Loading