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 @@ -212,6 +212,21 @@ export default class AttributeApplicationValidator implements AstValidator<Attri
}
}

@check('@regex')
private _checkRegex(attr: AttributeApplication, accept: ValidationAcceptor) {
const regex = getStringLiteral(attr.args[0]?.value);
if (regex === undefined) {
accept('error', `Expecting a string literal`, { node: attr.args[0] ?? attr });
return;
}

try {
new RegExp(regex);
} catch (e) {
accept('error', `${e}`, { node: attr.args[0] });
}
}

private rejectEncryptedFields(attr: AttributeApplication, accept: ValidationAcceptor) {
streamAllContents(attr).forEach((node) => {
if (isDataModelFieldReference(node) && hasAttribute(node.target.ref as DataModelField, '@encrypted')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1369,4 +1369,26 @@ describe('Attribute tests', () => {
`)
).resolves.toContain(`attribute "@omit" cannot be used on type declaration fields`);
});

it('validates regex', async () => {
await expect(
loadModelWithError(`
${prelude}
model User {
id String @id
phone String @regex(id)
}
`)
).resolves.toContain('Expecting a string literal');

await expect(
loadModelWithError(`
${prelude}
model User {
id String @id
phone String @regex("^(\\+46|0)[0-9]{7,12}$")
}
`)
).resolves.toContain('Invalid regular expression');
});
});
Loading