Skip to main content

Validate cart details

The validateCart mutation triggers different validations for the cart. Use this mutation to validate cart items individually or collectively.

Any asynchronous validations that cannot be completed within time are returned as pending validations. They can be fetched using a cart validation query at a later point in time. Cart validation query can be called periodically after running the validateCart mutation until all the pending validation are completed.

The validations can be performed at two levels:

  • PRE_FINALIZE -use this validation If there needs to be a pre-validation check for the item(s) and rest of the fields prior to saving the billing and shipping details.
  • PREVENT_FINALIZE - use this option to validate the cart before completing the purchase as it validates everything needed to complete the purchase.

Mutation:

mutation validateCart($input: CartValidationInput!) {
validateCart(input: $input) {
cartValidation {
id
blockingErrors {
itemId
code
message
messageParameters {
key
value
__typename
}
__typename
}
warnings {
itemId
message
__typename
}
info
pendingValidations
__typename
}
__typename
}
}

Sample arguments:

{
"input": {
"cartId": "6512b7ff875fe104e5fde6da",
"level": "PRE_FINALIZE",
"itemIds": ["cf39a936-efe1-434b-a7d4-79b61b6b97ca"],
"locale": "en-US"
}

Sample output:

{
"data": {
"validateCart": {
"cartValidation": {
"id": "d837a6c5-659c-4d5c-9c46-50230dc02cd1",
"blockingErrors": [
{
"itemId": "cf39a936-efe1-434b-a7d4-79b61b6b97ca",
"code": "REQUIRED_FIELD_VALIDATION_ERROR",
"message": "Required field 'firstName' has failed validation: Missing information",
"messageParameters": [
{
"key": "REQUIRED_FIELD_KEY",
"value": "firstName",
"__typename": "CartValidationMessageParameter"
}
]
}
]
}
}
}
}

The cartValidation query

Use the cartValidation query to get the details of the validation triggered previously by the validateCart mutation. This is useful when there are pending validations. Polling can be done at specific intervals until these pending validations are completed.

Query:

query cartValidation($input: CartValidationRequest!) {
cartValidation(input: $input) {
id
blockingErrors {
itemId
code
message
messageParameters {
key
value
__typename
}
__typename
}
warnings {
itemId
message
__typename
}
info
pendingValidations
__typename
}
__typename
}

Sample arguments:

{
"input": {
"cartId": "6512b7ff875fe104e5fde6da",
"level": "PRE_FINALIZE",
"itemIds": ["cf39a936-efe1-434b-a7d4-79b61b6b97ca"],
"locale": "en-US"
}

Sample output:

{
"data": {
"cartValidation": {
"id": "d837a6c5-659c-4d5c-9c46-50230dc02cd1",
"blockingErrors": [
{
"itemId": "cf39a936-efe1-434b-a7d4-79b61b6b97ca",
"code": "REQUIRED_FIELD_VALIDATION_ERROR",
"message": "Required field 'firstName' has failed validation: Missing information",
"messageParameters": [
{
"key": "REQUIRED_FIELD_KEY",
"value": "firstName",
"__typename": "CartValidationMessageParameter"
}
]
}
]
}
}
}
}

Common errors

For information on some of the common errors related to this mutation, refer to the section on Error handling.

Was this page helpful?