Skip to content

Instantly share code, notes, and snippets.

@hutch78
Created March 29, 2022 01:07
Show Gist options
  • Save hutch78/3718e7f057af8b19f070ac8005107744 to your computer and use it in GitHub Desktop.
Save hutch78/3718e7f057af8b19f070ac8005107744 to your computer and use it in GitHub Desktop.
Vue 3 \ Typescript \ Vuelidate Errors
<script lang="ts" setup>
const state = reactive<ShipmentManagerState>({
shipmentDetailsSubmitLoading: false,
readOnly: false,
booking: null,
cargoForm: {
params: {
cargo_year: '',
cargo_make: '',
cargo_model: '',
cargo_vin_serial: '',
cargo_value: '',
},
},
truckingForm: {
mode: 'new',
params: {
id: null,
nickname: '',
name: '',
phone_number: '',
email: '',
company: '',
address_ln1: '',
address_ln2: '',
city: '',
state: '',
country: '',
zipcode: '',
special_instructions: '',
loading_assistance: '',
displayLabel: '',
},
tempParams: {},
},
insuranceForm: {
params: {
has_insurance: null,
},
},
shipperForm: {
error: '',
loading: '',
mode: null,
open: false,
params: {
id: '',
name: '',
email: '',
phone_number: '',
company: '',
address_ln1: '',
address_ln2: '',
country: '',
city: '',
state: '',
zipcode: '',
tax_id: '',
is_shipper: '',
is_receiver: '',
selected: null,
},
},
receiverForm: {
error: '',
loading: '',
mode: null,
open: false,
params: {
id: '',
name: '',
email: '',
phone_number: '',
company: '',
address_ln1: '',
address_ln2: '',
country: '',
city: '',
state: '',
zipcode: '',
tax_id: '',
is_shipper: '',
is_receiver: '',
selected: null,
},
},
shipmentContactForm: {},
contacts: [],
shipperContacts: [],
receiverContacts: [],
shipmentTrucking: [],
originalValues: {
cargoForm: {},
truckingForm: {},
insuranceForm: {},
shipperForm: {},
receiverForm: {},
},
});
const rules = computed(() => {
return {
cargoForm: {
params: {
cargo_year: {
required,
numeric,
minValue: minValue(1800),
maxValue: maxValue(new Date().getFullYear() + 1),
},
cargo_make: { required },
cargo_model: { required },
cargo_vin_serial: { required },
cargo_value: { required, numeric },
},
},
insuranceForm: {
params: {
has_insurance: { required },
},
},
shipperForm: {
params: {
selected: { required },
},
},
receiverForm: {
params: {
selected: { required },
},
},
truckingForm: state.booking && state.booking.has_trucking ? {
params: {
name: { required },
phone_number: { required },
address_ln1: { required },
city: { required },
state: { required },
zipcode: { required },
},
} : null
}
});
const v$ = useVuelidate<typeof rules, ShipmentManagerState>(rules, state, {
$lazy: true,
$autoDirty: true,
});
</script>
export type ShipmentManagerState = {
shipmentDetailsSubmitLoading: Boolean;
readOnly: Boolean;
booking: Booking;
cargoForm: {
params: {
cargo_year: string;
cargo_make: string;
cargo_model: string;
cargo_vin_serial: string;
cargo_value: string;
};
};
truckingForm: {
mode: string;
params: {
id: number;
nickname: string;
name: string;
phone_number: string;
email: string;
company: string;
address_ln1: string;
address_ln2: string;
city: string;
state: string;
country: string;
zipcode: string;
special_instructions: string;
loading_assistance: string;
displayLabel: string; // several trucking fields including the name and address concatenated into a label string for use in dropdowns
};
tempParams: any;
};
insuranceForm: {
params: {
has_insurance: boolean | null;
};
};
shipperForm: {
error: string;
loading: string;
mode: null;
open: Boolean;
params: ShipmentContactFormParams;
};
receiverForm: {
error: string;
loading: string;
mode: null;
open: Boolean;
params: ShipmentContactFormParams;
};
shipmentContactForm: {};
contacts: Array<ShipmentContact>;
shipperContacts: Array<ShipmentContact>;
receiverContacts: Array<ShipmentContact>;
shipmentTrucking: Array<ShipmentTrucking>;
originalValues: {
cargoForm: any;
truckingForm: any;
insuranceForm: any;
shipperForm: any;
receiverForm: any;
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment