Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ardalanamini committed Dec 3, 2018
1 parent 4abc90f commit 823a33c
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 46 deletions.
49 changes: 15 additions & 34 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@foxify/odin",
"version": "0.5.2",
"version": "0.5.3",
"description": "Active Record Model",
"author": "Ardalan Amini <ardalanamini22@gmail.com> [https://github.com/ardalanamini]",
"contributors": [
Expand Down Expand Up @@ -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",
Expand Down
14 changes: 7 additions & 7 deletions src/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ class Base<T extends object = {}> {
return MODELS;
}

public static isOdin = (arg: any): arg is Odin => arg instanceof Odin;

public static register = (...models: Array<typeof Odin>) => {
models.forEach((model) => {
if (MODELS[model.name]) throw new Error(`Model "${model.name}" already exists`);
Expand All @@ -97,6 +95,13 @@ class Base<T extends object = {}> {
}

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[] = []) => {
Expand Down Expand Up @@ -205,11 +210,6 @@ class Base<T extends object = {}> {
};
});

if (definitions) return {
definitions: JSON_SCHEMA_DEFINITIONS,
...jsonSchema,
};

return jsonSchema;
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"];

Expand Down Expand Up @@ -36,8 +36,10 @@ class Odin<T extends object = {}> extends Relational<T> {
public static GraphQL = GraphQL;
public static Types = Types;

public static isOdin = (arg: any): arg is Odin => arg instanceof Odin;

public static on<T extends object>(event: Odin.Event, listener: (item: Odin<T>) => 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);

Expand Down
2 changes: 1 addition & 1 deletion src/types/String.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit 823a33c

Please sign in to comment.