> For the complete documentation index, see [llms.txt](https://devworkshops.gitbook.io/masteringvuejs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://devworkshops.gitbook.io/masteringvuejs/consuming-apis/server-side-validation.md).

# Server side validation

Just so we can test the server side validation, we're going to update our **Secured Mock API** to include some validation.

{% code title="auth-json-server.js" %}

```javascript
function validateSuppliers(req, res, next) {
    console.log('validating')
    const { address, companyName, contactName, contactTitle } = req.body
    var errors = {}

    if (!companyName) errors.companyName = 'Required'
    if (!contactName) errors.contactName = 'Required'
    if (!contactTitle) errors.contactTitle = 'Required'
    if (!address) errors.address = 'Required'
    else {
        if (!address.street) errors['address.street'] = 'Required'
        if (!address.city) errors['address.city'] = 'Required'
        if (!address.region) errors['address.region'] = 'Required'
        if (!address.postalCode) errors['address.postalCode'] = 'Required'
        if (!address.country) errors['address.country'] = 'Required'
        else {
            if (address.country.toLowerCase() == 'australia') {
                if (address.postalCode.length != 4) {
                    errors['address.postalCode'] = 'Invalid'
                }
            }
        }
    }
    if (Object.keys(errors).length > 0) {
        res.status(422).json({ errors })
    } else {
        next()
    }
}

server.post('/suppliers', validateSuppliers)
server.put(/^\/suppliers\/.*$/, validateSuppliers)
```

{% endcode %}

Now let's update the **SupplierEdit.vue** component to start using this server side validation. First we going to update the section where we call the service to update the supplier. We going to catch errors and if the status code is 422 it means validation error then we bind it to an errors variable in the data

{% code title="SupplierEdit.vue" %}

```javascript
SupplierService.update(this.model)
    .then(() => this.navigateBack())
    .catch(err => {
        if (err.response.status == 422) {
            this.errors = err.response.data.errors
        }
    })
```

{% endcode %}

Then in our template, we display the error message according to the field we're validating

{% code title="SupplierEdit.vue" %}

```markup
<div class="form-group">
    <label class="col-form-label">Company Name</label>
    <input type="text" class="form-control" 
        id="companyNameField" 
        v-model="model.companyName" 
        :class="{ 'is-invalid': errors && errors.companyName }">
    <div class="invalid-feedback" 
        v-if="errors && errors.companyName">{{ errors.companyName }}
    </div>
</div>
```

{% endcode %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://devworkshops.gitbook.io/masteringvuejs/consuming-apis/server-side-validation.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
