Skip to content

Commit

Permalink
Add Zod's built-in string transformations as field attributes (#981)
Browse files Browse the repository at this point in the history
Co-authored-by: Yiming <yiming@whimslab.io>
  • Loading branch information
simonedelmann and ymc9 authored Feb 3, 2024
1 parent 79ab755 commit dde1931
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/schema/src/plugins/zod/utils/schema-gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@ export function makeFieldSchema(field: DataModelField, respectDefault = false) {
schema += `.url(${messageArgFirst})`;
break;
}
case '@trim': {
schema += `.trim()`;
break;
}
case '@lower': {
schema += `.toLowerCase()`;
break;
}
case '@upper': {
schema += `.toUpperCase()`;
break;
}
case '@datetime': {
schema += `.datetime({ offset: true${message ? ', message: ' + JSON.stringify(message) : ''} })`;
break;
Expand Down
15 changes: 15 additions & 0 deletions packages/schema/src/res/stdlib.zmodel
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,21 @@ attribute @datetime(_ message: String?) @@@targetField([StringField]) @@@validat
*/
attribute @url(_ message: String?) @@@targetField([StringField]) @@@validation

/**
* Trims whitespaces from the start and end of the string.
*/
attribute @trim() @@@targetField([StringField]) @@@validation

/**
* Transform entire string toLowerCase.
*/
attribute @lower() @@@targetField([StringField]) @@@validation

/**
* Transform entire string toUpperCase.
*/
attribute @upper() @@@targetField([StringField]) @@@validation

/**
* Validates a number field is greater than the given value.
*/
Expand Down

0 comments on commit dde1931

Please sign in to comment.