@@ -17,6 +17,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
1717For commercial licensing, please contact support@quantumnous.com
1818*/
1919import { z } from 'zod'
20+ import type { TFunction } from 'i18next'
2021import { parseQuotaFromDollars , quotaUnitsToDollars } from '@/lib/format'
2122import { DEFAULT_GROUP } from '../constants'
2223import { type ApiKeyFormData , type ApiKey } from '../types'
@@ -25,19 +26,40 @@ import { type ApiKeyFormData, type ApiKey } from '../types'
2526// Form Schema
2627// ============================================================================
2728
28- export const apiKeyFormSchema = z . object ( {
29- name : z . string ( ) . min ( 1 , 'Name is required' ) ,
30- remain_quota_dollars : z . number ( ) . min ( 0 ) . optional ( ) ,
31- expired_time : z . date ( ) . optional ( ) ,
32- unlimited_quota : z . boolean ( ) ,
33- model_limits : z . array ( z . string ( ) ) ,
34- allow_ips : z . string ( ) . optional ( ) ,
35- group : z . string ( ) . optional ( ) ,
36- cross_group_retry : z . boolean ( ) . optional ( ) ,
37- tokenCount : z . number ( ) . min ( 1 ) . optional ( ) ,
38- } )
29+ export function getApiKeyFormSchema ( t : TFunction ) {
30+ return z
31+ . object ( {
32+ name : z . string ( ) . min ( 1 , t ( 'Please enter a name' ) ) ,
33+ remain_quota_dollars : z . number ( ) . optional ( ) ,
34+ expired_time : z . date ( ) . optional ( ) ,
35+ unlimited_quota : z . boolean ( ) ,
36+ model_limits : z . array ( z . string ( ) ) ,
37+ allow_ips : z . string ( ) . optional ( ) ,
38+ group : z . string ( ) . optional ( ) ,
39+ cross_group_retry : z . boolean ( ) . optional ( ) ,
40+ tokenCount : z . number ( ) . min ( 1 ) . optional ( ) ,
41+ } )
42+ . superRefine ( ( data , ctx ) => {
43+ if ( data . unlimited_quota ) {
44+ return
45+ }
3946
40- export type ApiKeyFormValues = z . infer < typeof apiKeyFormSchema >
47+ if (
48+ data . remain_quota_dollars === undefined ||
49+ data . remain_quota_dollars < 0
50+ ) {
51+ ctx . addIssue ( {
52+ code : 'custom' ,
53+ path : [ 'remain_quota_dollars' ] ,
54+ message : t ( 'Quota must be zero or greater' ) ,
55+ } )
56+ }
57+ } )
58+ }
59+
60+ export type ApiKeyFormValues = z . infer <
61+ ReturnType < typeof getApiKeyFormSchema >
62+ >
4163
4264// ============================================================================
4365// Form Defaults
@@ -100,7 +122,9 @@ export function transformApiKeyToFormDefaults(
100122) : ApiKeyFormValues {
101123 return {
102124 name : apiKey . name ,
103- remain_quota_dollars : quotaUnitsToDollars ( apiKey . remain_quota ) ,
125+ remain_quota_dollars : apiKey . unlimited_quota
126+ ? 0
127+ : quotaUnitsToDollars ( apiKey . remain_quota ) ,
104128 expired_time :
105129 apiKey . expired_time > 0
106130 ? new Date ( apiKey . expired_time * 1000 )
0 commit comments