Comparing version 0.2.0 to 0.3.0
@@ -0,1 +1,7 @@ | ||
# 0.3.0 | ||
* Add primary attribute option | ||
* Add unqiue attribute option | ||
* Add validation error and mapped model errors type | ||
# 0.2.0 | ||
@@ -2,0 +8,0 @@ |
@@ -30,3 +30,8 @@ import { Model, ModelConstructor } from './model'; | ||
constructor(type: AssociationType, name: string); | ||
compile(): string; | ||
/** | ||
* Turns the association into its relevant property name. | ||
* | ||
* @returns The property name. | ||
*/ | ||
toString(): string; | ||
} | ||
@@ -59,2 +64,2 @@ /** The options that can be defined for an association. */ | ||
*/ | ||
export declare function assoc<T extends Model>(type: AssociationType, target?: ModelConstructor<T>, options?: any): (ctor: Object, key: string | symbol) => void; | ||
export declare function assoc<T extends Model>(type: AssociationType, target?: ModelConstructor<T>, options?: AssociationOptions): (ctor: Object, key: string | symbol) => void; |
@@ -55,3 +55,3 @@ "use strict"; | ||
} | ||
/* | ||
/** | ||
* Turns the association into its relevant property name. | ||
@@ -61,3 +61,3 @@ * | ||
*/ | ||
Association.prototype.compile = function () { | ||
Association.prototype.toString = function () { | ||
return this.name; | ||
@@ -64,0 +64,0 @@ }; |
@@ -19,2 +19,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/* tslint:disable:completed-docs */ | ||
var chai = require("chai"); | ||
@@ -21,0 +22,0 @@ var association_1 = require("./association"); |
@@ -130,3 +130,8 @@ import { Property } from './property'; | ||
constructor(type: AttributeType, name: string); | ||
compile(): string; | ||
/** | ||
* Turns the attribute into its relevant property name. | ||
* | ||
* @returns The property name. | ||
*/ | ||
toString(): string; | ||
} | ||
@@ -137,2 +142,11 @@ /** The options that can be defined for an attribute. */ | ||
type?: AttributeType; | ||
/** | ||
* Whether the attribute should be optional. | ||
* Attributes are required by default. | ||
*/ | ||
optional?: boolean; | ||
/** Whether the attribute is a primary key. */ | ||
primary?: boolean; | ||
/** Whether the attribute is unique. */ | ||
unique?: boolean; | ||
} | ||
@@ -150,2 +164,2 @@ /** The attributes defined on a model. */ | ||
*/ | ||
export declare function attr(type: AttributeType, options?: any): (target: Object, key: string | symbol) => void; | ||
export declare function attr(type: AttributeType, options?: AttributeOptions): (target: Object, key: string | symbol) => void; |
@@ -153,3 +153,3 @@ "use strict"; | ||
} | ||
/* | ||
/** | ||
* Turns the attribute into its relevant property name. | ||
@@ -159,3 +159,3 @@ * | ||
*/ | ||
Attribute.prototype.compile = function () { | ||
Attribute.prototype.toString = function () { | ||
return this.name; | ||
@@ -162,0 +162,0 @@ }; |
@@ -19,2 +19,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/* tslint:disable:completed-docs */ | ||
var chai = require("chai"); | ||
@@ -21,0 +22,0 @@ var attribute_1 = require("./attribute"); |
@@ -73,2 +73,8 @@ /** Contains getters/setters for the model metadata. */ | ||
export declare function getAttributes(ctor: Function): ModelAttributes; | ||
/** | ||
* Get the model properties for a model constructor. | ||
* | ||
* @param ctor The model constructor. | ||
* @returns The model properties. | ||
*/ | ||
export declare function getProperties<T extends Model>(ctor: Function): ModelProperties<T>; |
@@ -125,2 +125,8 @@ "use strict"; | ||
exports.getAttributes = getAttributes; | ||
/** | ||
* Get the model properties for a model constructor. | ||
* | ||
* @param ctor The model constructor. | ||
* @returns The model properties. | ||
*/ | ||
function getProperties(ctor) { | ||
@@ -127,0 +133,0 @@ var props = {}; |
@@ -19,2 +19,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/* tslint:disable:completed-docs */ | ||
var chai = require("chai"); | ||
@@ -25,3 +26,3 @@ var attribute_1 = require("./attribute"); | ||
var model_1 = require("./model"); | ||
/* tslint:disable */ | ||
/* tslint:disable:class-name */ | ||
var ANiceModelName = (function () { | ||
@@ -42,3 +43,3 @@ function ANiceModelName() { | ||
}()); | ||
/* tslint:enable */ | ||
/* tslint:enable:class-name */ | ||
var Comment = (function (_super) { | ||
@@ -88,6 +89,6 @@ __extends(Comment, _super); | ||
var props = metadata_1.getProperties(User); | ||
chai.should().equal(props.name.compile(), 'name'); | ||
chai.should().equal(props.comments.compile(), 'comments'); | ||
chai.should().equal(props.name.toString(), 'name'); | ||
chai.should().equal(props.comments.toString(), 'comments'); | ||
}); | ||
}); | ||
//# sourceMappingURL=metadata.spec.js.map |
@@ -44,3 +44,3 @@ /** Contains the model classes and decorators. */ | ||
export declare type ModelProperties<T extends Model> = { | ||
[P in keyof T]?: Property<T[P]>; | ||
[P in keyof T]: Property<T[P]>; | ||
}; | ||
@@ -47,0 +47,0 @@ /** The options that can be defined for a model. */ |
@@ -45,3 +45,3 @@ "use strict"; | ||
var assocs = metadata_1.getAssociations(ctor); | ||
var key = prop.compile(); | ||
var key = prop.toString(); | ||
var options = assocs[key]; | ||
@@ -48,0 +48,0 @@ if (!type) { |
@@ -19,2 +19,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/* tslint:disable:completed-docs */ | ||
var chai = require("chai"); | ||
@@ -21,0 +22,0 @@ var association_1 = require("./association"); |
/** Contains the property type. */ | ||
/** A property of a model. This can be either an association or attribute. */ | ||
export declare abstract class Property<T> { | ||
abstract compile(): string; | ||
/** | ||
* Turns the property into its relevant property name. | ||
* | ||
* @returns The property name. | ||
*/ | ||
abstract toString(): string; | ||
} |
@@ -19,2 +19,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/* tslint:disable:completed-docs */ | ||
var chai = require("chai"); | ||
@@ -21,0 +22,0 @@ var attribute_1 = require("./attribute"); |
@@ -0,1 +1,2 @@ | ||
import { Model, ModelConstructor } from './model'; | ||
/** A type of validation on a model. */ | ||
@@ -65,2 +66,28 @@ export declare enum Validation { | ||
/** | ||
* Errors with a model. Each model property can have | ||
* an array of error messages. | ||
*/ | ||
export declare type ModelErrors<T extends Model> = { | ||
[P in keyof T]?: string[]; | ||
}; | ||
/** | ||
* An error with a model's validations. | ||
* This has a mapped errors object | ||
* so that you access the errors by a specific model property. | ||
*/ | ||
export declare class ValidationError<T extends Model> extends Error { | ||
/** The model constructor. */ | ||
ctor: ModelConstructor<T>; | ||
/** The model errors. */ | ||
errors: ModelErrors<T>; | ||
/** | ||
* Construct a validation error. | ||
* | ||
* @param ctor The model constructor this error is for. | ||
* @param message The error message. | ||
* @param err The model errors with all of the validation errors on it. | ||
*/ | ||
constructor(ctor: ModelConstructor<T>, message: string, errors: ModelErrors<T>); | ||
} | ||
/** | ||
* A custom validation function that can be used to validate | ||
@@ -67,0 +94,0 @@ * an attribute's value. |
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return function (d, b) { | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
}; | ||
})(); | ||
var __assign = (this && this.__assign) || Object.assign || function(t) { | ||
@@ -79,2 +89,27 @@ for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
/** | ||
* An error with a model's validations. | ||
* This has a mapped errors object | ||
* so that you access the errors by a specific model property. | ||
*/ | ||
var ValidationError = (function (_super) { | ||
__extends(ValidationError, _super); | ||
/** | ||
* Construct a validation error. | ||
* | ||
* @param ctor The model constructor this error is for. | ||
* @param message The error message. | ||
* @param err The model errors with all of the validation errors on it. | ||
*/ | ||
function ValidationError(ctor, message, errors) { | ||
var _this = _super.call(this, message) || this; | ||
_this.name = 'ValidationError'; | ||
_this.stack = new Error().stack; | ||
_this.ctor = ctor; | ||
_this.errors = errors; | ||
return _this; | ||
} | ||
return ValidationError; | ||
}(Error)); | ||
exports.ValidationError = ValidationError; | ||
/** | ||
* A decorator used on model attributes to define validation rules. | ||
@@ -81,0 +116,0 @@ * A message and arguments can be provided which will be passed to the equivalent |
{ | ||
"name": "modelsafe", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"main": "dist/index.js", | ||
@@ -24,3 +24,3 @@ "bugs": "https://github.com/creativecuriositystudio/modelsafe/issues", | ||
"test:auto": "mocha --opts mocha.opts --watch", | ||
"lint": "tslint src/**/*.ts", | ||
"lint": "tslint --format stylish --type-check --project tsconfig.json", | ||
"clean": "rm -rf dist" | ||
@@ -36,3 +36,3 @@ }, | ||
"tslint": "^4.0.2", | ||
"tslint-config-ccs": "^0.1.0", | ||
"tslint-config-ccs": "^0.2.0", | ||
"typedoc": "^0.5.1" | ||
@@ -39,0 +39,0 @@ }, |
@@ -0,1 +1,2 @@ | ||
/* tslint:disable:completed-docs */ | ||
import * as chai from 'chai'; | ||
@@ -2,0 +3,0 @@ |
@@ -46,3 +46,3 @@ /** Contains the association types. */ | ||
/* | ||
/** | ||
* Turns the association into its relevant property name. | ||
@@ -52,3 +52,3 @@ * | ||
*/ | ||
public compile(): string { | ||
public toString(): string { | ||
return this.name; | ||
@@ -87,4 +87,4 @@ } | ||
*/ | ||
export function assoc<T extends Model>(type: AssociationType, target?: ModelConstructor<T>, options?: any) { | ||
export function assoc<T extends Model>(type: AssociationType, target?: ModelConstructor<T>, options?: AssociationOptions) { | ||
return (ctor: Object, key: string | symbol) => defineAssociation(ctor, key, { ... options, type, target }); | ||
} |
@@ -0,1 +1,2 @@ | ||
/* tslint:disable:completed-docs */ | ||
import * as chai from 'chai'; | ||
@@ -2,0 +3,0 @@ |
@@ -185,3 +185,3 @@ /** Contains the attribute types. */ | ||
/* | ||
/** | ||
* Turns the attribute into its relevant property name. | ||
@@ -191,3 +191,3 @@ * | ||
*/ | ||
public compile(): string { | ||
public toString(): string { | ||
return this.name; | ||
@@ -201,2 +201,14 @@ } | ||
type?: AttributeType; | ||
/** | ||
* Whether the attribute should be optional. | ||
* Attributes are required by default. | ||
*/ | ||
optional?: boolean; | ||
/** Whether the attribute is a primary key. */ | ||
primary?: boolean; | ||
/** Whether the attribute is unique. */ | ||
unique?: boolean; | ||
} | ||
@@ -216,4 +228,4 @@ | ||
*/ | ||
export function attr(type: AttributeType, options?: any) { | ||
export function attr(type: AttributeType, options?: AttributeOptions) { | ||
return (target: Object, key: string | symbol) => defineAttribute(target, key, { ... options, type }); | ||
} |
@@ -0,1 +1,2 @@ | ||
/* tslint:disable:completed-docs */ | ||
import * as chai from 'chai'; | ||
@@ -8,7 +9,7 @@ | ||
/* tslint:disable */ | ||
/* tslint:disable:class-name */ | ||
class ANiceModelName {} | ||
class thisISA_WEIRD_ModelName {} | ||
class This_Is_Even_Weirder123 {} | ||
/* tslint:enable */ | ||
/* tslint:enable:class-name */ | ||
@@ -46,7 +47,7 @@ @model() | ||
it('should map properties correctly', () => { | ||
let props: ModelProperties<User> = getProperties(User); | ||
let props: ModelProperties<User> = getProperties<User>(User); | ||
chai.should().equal(props.name.compile(), 'name'); | ||
chai.should().equal(props.comments.compile(), 'comments'); | ||
chai.should().equal(props.name.toString(), 'name'); | ||
chai.should().equal(props.comments.toString(), 'comments'); | ||
}); | ||
}); |
@@ -137,2 +137,8 @@ /** Contains getters/setters for the model metadata. */ | ||
/** | ||
* Get the model properties for a model constructor. | ||
* | ||
* @param ctor The model constructor. | ||
* @returns The model properties. | ||
*/ | ||
export function getProperties<T extends Model>(ctor: Function): ModelProperties<T> { | ||
@@ -139,0 +145,0 @@ let props = {}; |
@@ -0,1 +1,2 @@ | ||
/* tslint:disable:completed-docs */ | ||
import * as chai from 'chai'; | ||
@@ -2,0 +3,0 @@ |
@@ -44,3 +44,3 @@ /** Contains the model classes and decorators. */ | ||
let assocs = getAssociations(ctor); | ||
let key = prop.compile(); | ||
let key = prop.toString(); | ||
let options = assocs[key]; | ||
@@ -65,3 +65,3 @@ | ||
export type ModelProperties<T extends Model> = { | ||
[P in keyof T]?: Property<T[P]>; | ||
[P in keyof T]: Property<T[P]>; | ||
}; | ||
@@ -68,0 +68,0 @@ |
@@ -5,3 +5,3 @@ /** Contains the property type. */ | ||
export abstract class Property<T> { | ||
/* | ||
/** | ||
* Turns the property into its relevant property name. | ||
@@ -11,3 +11,3 @@ * | ||
*/ | ||
abstract compile(): string; | ||
abstract toString(): string; | ||
} |
@@ -0,1 +1,2 @@ | ||
/* tslint:disable:completed-docs */ | ||
import * as chai from 'chai'; | ||
@@ -2,0 +3,0 @@ |
/** Contains the validation types. */ | ||
import { defineAttributeValidation } from './metadata'; | ||
import { Model, ModelConstructor } from './model'; | ||
@@ -71,2 +72,39 @@ /** A type of validation on a model. */ | ||
/** | ||
* Errors with a model. Each model property can have | ||
* an array of error messages. | ||
*/ | ||
export type ModelErrors<T extends Model> = { | ||
[P in keyof T]?: string[]; | ||
}; | ||
/** | ||
* An error with a model's validations. | ||
* This has a mapped errors object | ||
* so that you access the errors by a specific model property. | ||
*/ | ||
export class ValidationError<T extends Model> extends Error { | ||
/** The model constructor. */ | ||
ctor: ModelConstructor<T>; | ||
/** The model errors. */ | ||
errors: ModelErrors<T>; | ||
/** | ||
* Construct a validation error. | ||
* | ||
* @param ctor The model constructor this error is for. | ||
* @param message The error message. | ||
* @param err The model errors with all of the validation errors on it. | ||
*/ | ||
constructor(ctor: ModelConstructor<T>, message: string, errors: ModelErrors<T>) { | ||
super(message); | ||
this.name = 'ValidationError'; | ||
this.stack = new Error().stack; | ||
this.ctor = ctor; | ||
this.errors = errors; | ||
} | ||
} | ||
/** | ||
* A custom validation function that can be used to validate | ||
@@ -73,0 +111,0 @@ * an attribute's value. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
252328
4192