{"id":25432913,"url":"https://github.com/peerigon/alamid-schema","last_synced_at":"2025-06-14T17:36:57.464Z","repository":{"id":11074110,"uuid":"13418737","full_name":"peerigon/alamid-schema","owner":"peerigon","description":"Extendable mongoose-like schemas for node.js and the browser","archived":false,"fork":false,"pushed_at":"2017-02-17T09:28:51.000Z","size":80,"stargazers_count":14,"open_issues_count":13,"forks_count":2,"subscribers_count":16,"default_branch":"master","last_synced_at":"2025-01-15T15:50:40.251Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/peerigon.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-10-08T16:16:52.000Z","updated_at":"2017-02-02T00:56:16.000Z","dependencies_parsed_at":"2022-09-04T00:10:20.879Z","dependency_job_id":null,"html_url":"https://github.com/peerigon/alamid-schema","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peerigon%2Falamid-schema","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peerigon%2Falamid-schema/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peerigon%2Falamid-schema/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/peerigon%2Falamid-schema/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/peerigon","download_url":"https://codeload.github.com/peerigon/alamid-schema/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239132559,"owners_count":19587106,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-02-17T05:17:15.282Z","updated_at":"2025-02-17T05:17:16.237Z","avatar_url":"https://github.com/peerigon.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"alamid-schema\n=============\n**Extendable mongoose-like schemas for node.js and the browser**\n\n[![npm status](https://nodei.co/npm/alamid-schema.svg?downloads=true\u0026stars=true)](https://npmjs.org/package/alamid-schema)\u003cbr\u003e\n[![build status](https://travis-ci.org/peerigon/alamid-schema.svg)](http://travis-ci.org/peerigon/alamid-schema)\n[![dependencies](https://david-dm.org/peerigon/alamid-schema.svg)](http://david-dm.org/peerigon/alamid-schema)\n[![devDependency Status](https://david-dm.org/peerigon/alamid-schema/dev-status.svg)](https://david-dm.org/peerigon/alamid-schema#info=devDependencies)\n\nIf you like [mongoose](http://mongoosejs.com/) schemas and you want to use them standalone, **alamid-schema** is the right module for you.\n\n__alamid-schema__ helps you with\n\n- validation of data\n- using mongoose-like schemas without using mongoose\n- sharing data-definition between client \u0026 server\n- normalizing data (coming soon)\n- striping readable/writeable fields (coming soon)\n\nUse it on the server to...\n - normalize and validate incoming requests\n - strip private fields from the response\n\nUse it on the client to...\n- validate forms\n- define view models\n\n```javascript\nvar Schema = require(\"alamid-schema\");\n\nvar Panda = new Schema(\"Panda\", {\n    name: String,\n    age: {\n        type: Number,\n        required: true,\n        writable: false,\n        readable: true\n    },\n    mood: {\n        type: String,\n        enum: [\"grumpy\", \"happy\"]\n    },\n    birthday: Date\n});\n```\n\n## Examples\n\n### Schema Definition\n\nYou can define your schema with concrete values...\n\n```javascript\nvar PandaSchema = new Schema({\n    name: \"panda\",\n    age: 12,\n    friends: {\n        type: []\n    }\n});\n```\n\n...or with abstract types...\n\n```javascript\nvar PandaSchema = new Schema({\n    name: String,\n    age: Number,\n    friends: {\n        type: Array\n    }\n});\n```\n\n### Extend\n\nSometimes you want to extend your Schema and add new properties.\n\n\n```javascript\nvar PandaSchema = new Schema({\n    name: String,\n    age: Number,\n    friends: {\n        type: Array\n    }\n});\n\nvar SuperPanda = PandaSchema.extend(\"SuperPanda\", {\n    xRay: true,\n    canFly: {\n        type: Boolean\n    }\n});\n```\n\nWe have a superpanda now... which can fly and has xray eyes!\nThat's basically the same as...\n\n```javascript\nvar SuperPanda = new Schema({\n    name: String,\n    age: Number,\n    friends: {\n        type: Array\n    },\n    xRay: true, //added\n    canFly: {   //added\n        type: Boolean\n    }\n});\n```\n\n\n__Overwriting properties__\n\nIf you define a property in the schema you are extending with, the extending schema takes precedence.\n\n\n```javascript\nvar Animal = new Schema({\n    name: String,\n    age: String\n});\n\nvar Panda = Animal.extend(\"Panda\", {\n    age: Number\n    color: String\n});\n```\n\nequals...\n\n```javascript\nvar Panda = new Schema(\"Panda\", {\n    name: String,\n    age: Number,   //overwritten\n    color: String  //added\n});\n```\n\n### Plugin: Validation\n\nThe validation plugins adds - *suprise!* - validation support.\n\n```javascript\nvar Schema = require(\"alamid-schema\");\n\nSchema.use(require(\"alamid-schema/plugins/validation\"));\n\nvar PandaSchema = new Schema({\n    name: {\n        type: String,\n        required: true\n    },\n    age: {\n        type: Number,\n        min: 9,\n        max: 99\n    },\n    mood: {\n        type: String,\n        enum: [\"happy\", \"sleepy\"]\n    },\n    treasures: {\n        type: Array,\n        minLength: 3\n    },\n    birthday: Date\n});\n\nvar panda = {\n    name: \"Hugo\",\n    age: 3,\n    mood: \"happy\"\n};\n\nPandaSchema.validate(panda, function(validation) {\n    if (!validation.result) {\n        console.log(validation.errors);\n        return;\n    }\n\n    console.log(\"happy panda\");\n});\n```\n\noutputs...\n\n```javascript\n{\n    result: false,\n    errors: {\n        age: [ 'min' ]\n    }\n}\n```\n\n\n_Included validators:_\n\n- required\n- min (works on Number)\n- max (works on Number)\n- enum\n- minLength (works on String, Array)\n- maxLength (works on String, Array)\n- hasLength (works on String, Array)\n- matches (performs a strict comparison, also accepts RegExp)\n\n_Writing custom validators:_\n\nYou can write sync and async validators..\n\n```javascript\n\n// sync\nfunction oldEnough(age) {\n    return age \u003e 18 || \"too-young\";\n}\n\n// async\nfunction nameIsUnique(name, callback) {\n    fs.readFile(__dirname + \"/names.json\", function(err, names) {\n        if(err) {\n            throw err;\n        }\n\n        names = JSON.parse(names);\n        callback(names.indexOf(name) === -1 || \"name-already-taken\");\n    });\n}\n\nvar PandaSchema = new Schema({\n    name: {\n        type: String,\n        required: true,\n        validate: nameIsUnique\n    },\n    age: {\n        type: Number,\n        validate: oldEnough,\n        max: 99\n    }\n});\n\nvar panda = {\n    name: \"hugo\",\n    age: 3,\n    mood: \"happy\"\n};\n\nPandaSchema.validate(panda, function(validation) {\n    if(!validation.result) {\n        console.log(validation.errors);\n        return;\n    }\n    console.log(\"happy panda\");\n});\n```\n\noutputs...\n\n\n```javascript\n{\n    name: [ \"name-already-taken\" ],\n    age:  [ \"too-young\" ]\n}\n```\n\n_Note:_ validators will be called with `this` bound to `model`.\n\n### Promises\n\nThe `validate()` method also returns a promise:\n\n```javascript\nPandaSchema.validate(panda)\n\t.then(function (validation) {\n\t\t...\n\t})\n```\n\nThe promise will still be resolved even when the validation fails, because a failed validation is\nnot an error, it's an expected state. \n\nThe promise provides a reference to the final validation result object. It contains the intermediate\nresult of all synchronous validators:\n\n```javascript\nvar promise = PandaSchema.validate(panda);\n\nif (promise.validation.result) {\n    console.log(\"Synchronous validation of \" + Object.keys(validation.errors) + \" failed\");\n}\n```\n\n__Important notice:__ You must bring your own ES6 Promise compatible polyfill!\n\n## API\n\n### Core\n\n#### Schema([name: String, ]definition: Object)\n\nCreates a new schema.\n\n#### .fields: Array\n\nThe array of property names that are defined on the schema. Do not modify this array.\n\n#### .only(key1: Array|String[, key2: String, key3: String, ...]): Schema\n\nReturns a subset with the given keys of the current schema. You may pass an array with keys or just the keys as arguments.\n\n#### .extend([name: String, ]definition: Object): Schema\n\nCreates a new schema that inherits from the current schema. Field definitions are merged where appropriate.\nIf a definition conflicts with the parent definition, the child's definition supersedes.\n\n#### .strip(model: Object)\n\nRemoves all properties from `model` that are not defined in `fields`. Will not remove properties that are inherited from the prototype.\n\n### Readable \u0026 Writable fields\n\nYou can define readable and writable fields in the schema. By default, every field is read- and writable.\n\n```javascript\nvar PandaSchema = new Schema({\n    id: {\n        type: Number,\n        required: true,\n        readable: true,\n        writable: false\n    },\n    lastModified: {\n        readable: true,\n        writable: false\n    }\n};\n```\n\n#### .writableFields()\n\nReturns an array containing the keys of all writable fields:\n\n```javascript\nPandaSchema.writableFields(); // [\"name\", \"age\", \"mood\", \"treasures\", \"birthday\"]\n```\n\n#### .writable()\n\nCreates a new schema that contains only the writable fields:\n\n```javascript\nvar PandaWritableSchema = PandaSchema.writable();\n```\n\n#### .readableFields()\n\nReturns an array containing the keys of all readable fields:\n\n```javascript\nPandaSchema.readableFields(); // [\"name\", \"age\", \"mood\", \"treasures\", \"birthday\"]\n```\n\n#### .readable()\n\nCreates a new schema that contains only the readable fields:\n\n```javascript\nvar PandaReadableSchema = PandaSchema.readable();\n```\n\n### Plugin: Validation\n\n#### .validate(model: Object[, callback: Function]): Promise\n\nValidate given model using the schema definitions. Callback will be called/Promise will be fulfilled with a validation object with `result` (Boolean) and `errors` (Object) containing the error codes.\n\n## Sponsors\n\n[\u003cimg src=\"https://assets.peerigon.com/peerigon/logo/peerigon-logo-flat-spinat.png\" width=\"150\" /\u003e](https://peerigon.com)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpeerigon%2Falamid-schema","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpeerigon%2Falamid-schema","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpeerigon%2Falamid-schema/lists"}