> For the complete documentation index, see [llms.txt](https://sebloix.gitbook.io/gstore-node/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sebloix.gitbook.io/gstore-node/v5.x+v6.x/schema/value_validation.md).

# Value validation

gstore uses the great validator library (<https://github.com/chriso/validator.js>) to validate input values so you can use any of the validations from that library.

```javascript
const entitySchema = new Schema({
    email  : { validate: 'isEmail' },
    website: { validate: 'isURL '},
    color  : { validate: 'isHexColor' },
    ...
});
```

If you need to use a function that requires extra arguments (e.g. [isIP](https://github.com/chriso/validator.js#validators)), you can define the `validate` in the following way:

```javascript
const entitySchema = new Schema({
  // ...
  ip  : {
    validate: {
      rule: 'isIP',
      args: [4]
    },
  }
});
```

This way, the `args` item will be passed to the as 3rd argument on the `isIP` validation function, accepting only IPs of IPv4 type.

## Custom validation function

If you have an embedded entity and you want to validate its properties you need to create a **custom validation function** for that. You then set your function as a "rule" and you can also pass additional arguments.\
Your custom validation function will receive as first argument the **value**, as second argument the **validator.js** *instance* and then the arguments you define.

```javascript
const entitySchema = new Schema({
  // ...
  propWithEmbedded: {
    validate: {
      rule: customValidationFunction, // you custom validation function
      args: [2, 8] // will be passed as "min" and "max" below
    },
  }
});

function customValidationFunction(obj, validator, min, max) {
    if ('embeddedEntity' in obj) {
        const { value } = obj.embeddedEntity;
        return validator.isNumeric(value.toString()) && (value >= min) && (value <= max);
    }

    return false;
}

// You can now validate an entity with this data
const data = {
  propWithEmbedded: {
    embeddedEntity: {
      value: 6,
    },
  },
};
const model = new MyModel(data);

model.save()
      .then(() => { ... })
      .catch((err) => {
          // If there is any validation error while saving
          // it will be returned here
      });
```

## Valid values

You can also define an **Array of valid values** for a properties.\
If you then try to save an entity with any other value it won't validate and won't be saved in the Datastore.

```javascript
const productSchema = new Schema({
    color  : { values: [ '#ffffff', '#ff6000', '#000000' ] },
    ...
});

const data = { color: '#cccccc' };
const product = new Product(data);
product.save().catch((err) => {
  // Validation Error
})
```


---

# 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:

```
GET https://sebloix.gitbook.io/gstore-node/v5.x+v6.x/schema/value_validation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
