diff --git a/package-lock.json b/package-lock.json index bf8a29e..4b55ced 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@foxify/odin", - "version": "0.5.2", + "version": "0.5.3", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -2098,8 +2098,7 @@ "ansi-regex": { "version": "2.1.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "aproba": { "version": "1.2.0", @@ -2120,14 +2119,12 @@ "balanced-match": { "version": "1.0.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, "dev": true, - "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -2142,20 +2139,17 @@ "code-point-at": { "version": "1.1.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "concat-map": { "version": "0.0.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "console-control-strings": { "version": "1.1.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "core-util-is": { "version": "1.0.2", @@ -2272,8 +2266,7 @@ "inherits": { "version": "2.0.3", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "ini": { "version": "1.3.5", @@ -2285,7 +2278,6 @@ "version": "1.0.0", "bundled": true, "dev": true, - "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -2300,7 +2292,6 @@ "version": "3.0.4", "bundled": true, "dev": true, - "optional": true, "requires": { "brace-expansion": "^1.1.7" } @@ -2308,14 +2299,12 @@ "minimist": { "version": "0.0.8", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "minipass": { "version": "2.2.4", "bundled": true, "dev": true, - "optional": true, "requires": { "safe-buffer": "^5.1.1", "yallist": "^3.0.0" @@ -2334,7 +2323,6 @@ "version": "0.5.1", "bundled": true, "dev": true, - "optional": true, "requires": { "minimist": "0.0.8" } @@ -2415,8 +2403,7 @@ "number-is-nan": { "version": "1.0.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "object-assign": { "version": "4.1.1", @@ -2428,7 +2415,6 @@ "version": "1.4.0", "bundled": true, "dev": true, - "optional": true, "requires": { "wrappy": "1" } @@ -2514,8 +2500,7 @@ "safe-buffer": { "version": "5.1.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "safer-buffer": { "version": "2.1.2", @@ -2551,7 +2536,6 @@ "version": "1.0.2", "bundled": true, "dev": true, - "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -2571,7 +2555,6 @@ "version": "3.0.1", "bundled": true, "dev": true, - "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -2615,14 +2598,12 @@ "wrappy": { "version": "1.0.2", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "yallist": { "version": "3.0.2", "bundled": true, - "dev": true, - "optional": true + "dev": true } } }, @@ -4186,9 +4167,9 @@ } }, "mongodb-memory-server": { - "version": "2.7.3", - "resolved": "https://registry.npmjs.org/mongodb-memory-server/-/mongodb-memory-server-2.7.3.tgz", - "integrity": "sha512-VZ7jECwVgXacKgUM3OHP2h1UToQpYDkCb9KLTdoC4I/6kDHTRIGEXUBfk4sw0YcDCELxhDPi4TBwHW2M9K5txw==", + "version": "2.7.4", + "resolved": "https://registry.npmjs.org/mongodb-memory-server/-/mongodb-memory-server-2.7.4.tgz", + "integrity": "sha512-oxMd9is2vPu+ORdnFqh6hVHwXyBPYeBPXlySG4Chl0uHMtx77HKDbcFDol9wz7ehhp2d3JJNLKsIJ8cpfGj6vA==", "dev": true, "requires": { "@babel/runtime": "^7.1.2", diff --git a/package.json b/package.json index 170ba67..6ace9cc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@foxify/odin", - "version": "0.5.2", + "version": "0.5.3", "description": "Active Record Model", "author": "Ardalan Amini [https://github.com/ardalanamini]", "contributors": [ @@ -63,7 +63,7 @@ "dotenv": "^6.1.0", "fs-readdir-recursive": "^1.1.0", "jest": "^23.6.0", - "mongodb-memory-server": "^2.7.3", + "mongodb-memory-server": "^2.7.4", "rimraf": "^2.6.2", "ts-jest": "^23.10.5", "tslint": "^5.11.0", diff --git a/src/Base.ts b/src/Base.ts index caf189c..57a77bd 100644 --- a/src/Base.ts +++ b/src/Base.ts @@ -76,8 +76,6 @@ class Base { return MODELS; } - public static isOdin = (arg: any): arg is Odin => arg instanceof Odin; - public static register = (...models: Array) => { models.forEach((model) => { if (MODELS[model.name]) throw new Error(`Model "${model.name}" already exists`); @@ -97,6 +95,13 @@ class Base { } public static toJsonSchema(definitions = true) { + if (definitions) return { + definitions: JSON_SCHEMA_DEFINITIONS, + ref: { + $ref: `#/definitions/${this.name}`, + }, + }; + const hidden = this.hidden; const jsonSchemaGenerator = (schema: Odin.Schema, ancestors: string[] = []) => { @@ -205,11 +210,6 @@ class Base { }; }); - if (definitions) return { - definitions: JSON_SCHEMA_DEFINITIONS, - ...jsonSchema, - }; - return jsonSchema; } } diff --git a/src/index.ts b/src/index.ts index bdb52ee..87e36fe 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,7 +5,7 @@ import events from "./events"; import GraphQL from "./GraphQL"; import * as Types from "./types"; import TypeAny from "./types/Any"; -import { array, define, getGetterName, getSetterName, object, string } from "./utils"; +import { define, getGetterName, getSetterName, object, string } from "./utils"; const EVENTS: Odin.Event[] = ["create"]; @@ -36,8 +36,10 @@ class Odin extends Relational { public static GraphQL = GraphQL; public static Types = Types; + public static isOdin = (arg: any): arg is Odin => arg instanceof Odin; + public static on(event: Odin.Event, listener: (item: Odin) => void) { - if (!array.contains(EVENTS, event)) throw new TypeError(`Unexpected event "${event}"`); + if (!EVENTS.includes(event)) throw new TypeError(`Unexpected event "${event}"`); events.on(`${this.name}:${event}`, listener); diff --git a/src/types/String.ts b/src/types/String.ts index d010329..8c9398a 100644 --- a/src/types/String.ts +++ b/src/types/String.ts @@ -47,7 +47,7 @@ class TypeString extends TypeAny { } get email() { - return this._test((v: string) => !/^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/.test(v) + return this._test((v: string) => !/^\w[\w\.]+@\w+?\.[a-zA-Z]{2,3}$/.test(v) ? "Must be an email address" : null); }