import {schemaWithName, InferSchemaType} from '@orion-js/schema'
export const PaymentSchema = schemaWithName('PaymentSchema', {
method: {
type: String,
allowedValues: ['credit_card', 'bank_transfer', 'paypal']
},
// Credit card details, required only when method is 'credit_card'
cardNumber: {
type: String,
optional: true,
validate: (value, {doc}) => {
if (doc.method === 'credit_card' && !value) {
return 'required-for-credit-card'
}
if (value && !/^\d{16}$/.test(value)) {
return 'invalid-card-number'
}
}
},
// PayPal email, required only when method is 'paypal'
paypalEmail: {
type: String,
optional: true,
validate: (value, {doc}) => {
if (doc.method === 'paypal' && !value) {
return 'required-for-paypal'
}
}
}
})
export type PaymentType = InferSchemaType<typeof PaymentSchema>