gstore-node
v8.x
v8.x
  • Introduction
  • Getting Started
    • Motivation
    • Installation
    • Create a gstore instance
  • Schema
    • About
    • Type validation
    • Value validation
    • Additional properties settings
    • Schema options
    • Joi Schema
    • Methods
      • path()
      • virtual()
    • Custom methods
  • Model
    • Creation
    • Methods
      • GET
      • UPDATE
      • DELETE
      • excludeFromIndexes()
      • key()
      • sanitize()
      • clearCache()
  • Entity
    • Creation
    • Properties
    • Methods
      • SAVE
      • plain()
      • populate()
      • model()
      • datastoreEntity()
      • validate()
  • Queries
    • @google-cloud Query
    • list()
    • findOne()
    • deleteAll()
    • findAround()
  • Populate
  • Middleware (hooks)
    • About
    • Pre hooks
    • Post hooks
  • Cache / Dataloader
    • Dataloader
    • Cache
  • gstore Methods
    • save()
  • Typescript
  • Appendix
    • Error Codes
    • Credits
Powered by GitBook
On this page
  • Custom validation function
  • Valid values

Was this helpful?

  1. Schema

Value validation

PreviousType validationNextAdditional properties settings

Last updated 6 years ago

Was this helpful?

gstore uses the great validator library () to validate input values so you can use any of the validations from that library.

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. ), you can define the validate in the following way:

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.

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.

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

const data = { color: '#cccccc' };
const product = new Product(data);
product.save().catch((err) => {
  // Validation Error
})
https://github.com/chriso/validator.js
isIP