Skip to content

Flexible Schema Validation

FLASH DB gives you the best of both worlds: 100% Schema Freedom (Schema-less NoSQL) by default, with the ability to define Optional Ergonomic Schema Validation Rules on specific collections.


Defining a Flexible Schema

You can define rules directly when instantiating a collection:

javascript
import { FlashClient } from 'flash-zk';

const client = new FlashClient({ secretKey: 'master_key' });

const users = client.collection('users', {
  schema: {
    name: { 
      type: 'string', 
      required: true, 
      min: 2, 
      max: 100 
    },
    email: { 
      type: 'string', 
      required: true, 
      match: /^[^\s@]+@[^\s@]+\.[^\s@]+$/ 
    },
    age: { 
      type: 'number', 
      min: 18, 
      max: 120 
    },
    role: { 
      type: 'string', 
      enum: ['admin', 'editor', 'viewer'], 
      default: 'viewer' 
    },
    isActive: { 
      type: 'boolean', 
      default: true 
    }
  }
});

Supported Rule Properties

PropertyTypeDescription
type'string' | 'number' | 'boolean' | 'array' | 'object'Required data type.
requiredbooleanRejects document if field is missing or empty.
defaultany | FunctionDefault value or factory function applied if omitted.
minnumberMinimum numerical value or minimum string length.
maxnumberMaximum numerical value or maximum string length.
matchRegExpRegular expression pattern for strings.
enumArray<any>Allowed list of values.
validate(value) => boolean | stringCustom synchronous validation logic.

Released under the Apache 2.0 License.