|
| 1 | +import { ObjectSchema, Field } from '@objectstack/spec/data'; |
| 2 | +import { ValidationRuleSchema } from '@objectstack/spec/data'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Event Object - Demonstrates Cross-Field Validation |
| 6 | + * |
| 7 | + * This example shows cross-field validation capabilities: |
| 8 | + * - Date range validation (end_date > start_date) |
| 9 | + * - Capacity validation (attendees <= max_capacity) |
| 10 | + * - Price validation (discount < total_price) |
| 11 | + */ |
| 12 | +export const Event = ObjectSchema.create({ |
| 13 | + name: 'event', |
| 14 | + label: 'Event', |
| 15 | + icon: 'calendar', |
| 16 | + nameField: 'title', |
| 17 | + enable: { |
| 18 | + apiEnabled: true, |
| 19 | + trackHistory: true, |
| 20 | + }, |
| 21 | + fields: { |
| 22 | + title: Field.text({ |
| 23 | + required: true, |
| 24 | + label: 'Event Title', |
| 25 | + maxLength: 200, |
| 26 | + }), |
| 27 | + |
| 28 | + description: Field.richtext({ |
| 29 | + label: 'Description', |
| 30 | + description: 'Event description with formatting', |
| 31 | + }), |
| 32 | + |
| 33 | + // Date fields for cross-field validation |
| 34 | + start_date: Field.datetime({ |
| 35 | + label: 'Start Date', |
| 36 | + required: true, |
| 37 | + }), |
| 38 | + |
| 39 | + end_date: Field.datetime({ |
| 40 | + label: 'End Date', |
| 41 | + required: true, |
| 42 | + }), |
| 43 | + |
| 44 | + // Capacity fields for validation |
| 45 | + max_capacity: Field.number({ |
| 46 | + label: 'Maximum Capacity', |
| 47 | + required: true, |
| 48 | + min: 1, |
| 49 | + }), |
| 50 | + |
| 51 | + current_attendees: Field.number({ |
| 52 | + label: 'Current Attendees', |
| 53 | + defaultValue: 0, |
| 54 | + min: 0, |
| 55 | + }), |
| 56 | + |
| 57 | + // Pricing fields |
| 58 | + ticket_price: Field.currency({ |
| 59 | + label: 'Ticket Price', |
| 60 | + required: true, |
| 61 | + min: 0, |
| 62 | + precision: 10, |
| 63 | + scale: 2, |
| 64 | + }), |
| 65 | + |
| 66 | + discount_amount: Field.currency({ |
| 67 | + label: 'Discount Amount', |
| 68 | + min: 0, |
| 69 | + precision: 10, |
| 70 | + scale: 2, |
| 71 | + }), |
| 72 | + |
| 73 | + // Location |
| 74 | + venue_address: Field.address({ |
| 75 | + label: 'Venue Address', |
| 76 | + required: true, |
| 77 | + addressFormat: 'us', |
| 78 | + }), |
| 79 | + |
| 80 | + venue_location: Field.geolocation({ |
| 81 | + label: 'Venue GPS Location', |
| 82 | + displayMap: true, |
| 83 | + allowGeocoding: true, |
| 84 | + }), |
| 85 | + |
| 86 | + status: Field.select({ |
| 87 | + label: 'Status', |
| 88 | + options: [ |
| 89 | + { label: 'Draft', value: 'draft', color: '#AAAAAA', default: true }, |
| 90 | + { label: 'Published', value: 'published', color: '#00AA00' }, |
| 91 | + { label: 'Cancelled', value: 'cancelled', color: '#AA0000' }, |
| 92 | + { label: 'Completed', value: 'completed', color: '#0000AA' }, |
| 93 | + ], |
| 94 | + }), |
| 95 | + }, |
| 96 | + |
| 97 | + // Cross-field validation rules |
| 98 | + validation: [ |
| 99 | + // 1. End date must be after start date |
| 100 | + { |
| 101 | + type: 'cross_field', |
| 102 | + name: 'end_after_start', |
| 103 | + condition: 'end_date > start_date', |
| 104 | + fields: ['start_date', 'end_date'], |
| 105 | + message: 'End date must be after start date', |
| 106 | + severity: 'error', |
| 107 | + active: true, |
| 108 | + }, |
| 109 | + |
| 110 | + // 2. Current attendees cannot exceed max capacity |
| 111 | + { |
| 112 | + type: 'cross_field', |
| 113 | + name: 'attendees_within_capacity', |
| 114 | + condition: 'current_attendees <= max_capacity', |
| 115 | + fields: ['current_attendees', 'max_capacity'], |
| 116 | + message: 'Current attendees cannot exceed maximum capacity', |
| 117 | + severity: 'error', |
| 118 | + active: true, |
| 119 | + }, |
| 120 | + |
| 121 | + // 3. Discount cannot exceed ticket price |
| 122 | + { |
| 123 | + type: 'cross_field', |
| 124 | + name: 'discount_within_price', |
| 125 | + condition: 'discount_amount <= ticket_price', |
| 126 | + fields: ['discount_amount', 'ticket_price'], |
| 127 | + message: 'Discount amount cannot exceed ticket price', |
| 128 | + severity: 'error', |
| 129 | + active: true, |
| 130 | + }, |
| 131 | + |
| 132 | + // 4. Warn when approaching capacity |
| 133 | + { |
| 134 | + type: 'cross_field', |
| 135 | + name: 'approaching_capacity', |
| 136 | + condition: '(current_attendees / max_capacity) < 0.9', |
| 137 | + fields: ['current_attendees', 'max_capacity'], |
| 138 | + message: 'Event is approaching maximum capacity (90% full)', |
| 139 | + severity: 'warning', |
| 140 | + active: true, |
| 141 | + }, |
| 142 | + |
| 143 | + // 5. Event must be at least 1 hour long |
| 144 | + { |
| 145 | + type: 'cross_field', |
| 146 | + name: 'minimum_duration', |
| 147 | + condition: '(end_date - start_date) >= 3600', // 3600 seconds = 1 hour |
| 148 | + fields: ['start_date', 'end_date'], |
| 149 | + message: 'Event must be at least 1 hour in duration', |
| 150 | + severity: 'error', |
| 151 | + active: true, |
| 152 | + }, |
| 153 | + |
| 154 | + // 6. Conditional validation: Published events require venue location |
| 155 | + { |
| 156 | + type: 'conditional', |
| 157 | + name: 'published_requires_location', |
| 158 | + when: 'status = "published"', |
| 159 | + message: 'Published events must have venue location', |
| 160 | + then: { |
| 161 | + type: 'script', |
| 162 | + name: 'venue_location_required', |
| 163 | + condition: 'venue_location = null OR venue_location = ""', |
| 164 | + message: 'Venue location is required for published events', |
| 165 | + severity: 'error', |
| 166 | + active: true, |
| 167 | + }, |
| 168 | + active: true, |
| 169 | + }, |
| 170 | + ], |
| 171 | +}); |
| 172 | + |
| 173 | +/** |
| 174 | + * Example Usage: |
| 175 | + * |
| 176 | + * // Valid event |
| 177 | + * const validEvent = { |
| 178 | + * title: 'Tech Conference 2024', |
| 179 | + * start_date: '2024-06-01T09:00:00Z', |
| 180 | + * end_date: '2024-06-01T17:00:00Z', // ✓ After start_date |
| 181 | + * max_capacity: 500, |
| 182 | + * current_attendees: 250, // ✓ Less than max_capacity |
| 183 | + * ticket_price: 100.00, |
| 184 | + * discount_amount: 20.00, // ✓ Less than ticket_price |
| 185 | + * status: 'published', |
| 186 | + * }; |
| 187 | + * |
| 188 | + * // Invalid event - validation errors |
| 189 | + * const invalidEvent = { |
| 190 | + * title: 'Invalid Event', |
| 191 | + * start_date: '2024-06-01T09:00:00Z', |
| 192 | + * end_date: '2024-06-01T08:00:00Z', // ✗ Before start_date |
| 193 | + * max_capacity: 100, |
| 194 | + * current_attendees: 150, // ✗ Exceeds max_capacity |
| 195 | + * ticket_price: 50.00, |
| 196 | + * discount_amount: 75.00, // ✗ Exceeds ticket_price |
| 197 | + * }; |
| 198 | + */ |
0 commit comments