This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
setProps<T extends BaseObject>(values: PartialOfProperties<T>): void { | |
for (let prop in values) { | |
this[prop] = | |
values[prop] !== undefined ? values[prop] : this[prop]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { NonFunctionKeys } from "utility-types" | |
export type PartialOfProperties<T extends object> = Partial<{ [P in NonFunctionKeys<T>]: T[P] }> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { NonFunctionKeys } from "utility-types" | |
export type OnlyProperties<T extends object> = { [P in NonFunctionKeys<T>]: T[P] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
setProps<T extends BaseObject>(values: T): void { | |
//... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@schema(personSchema, true) | |
class Person { | |
@propertySchema(personAgeSchema) | |
age: number | |
@propertySchema(personNameSchema) | |
name: string | |
constructor( | |
@paramSchema(personNameSchema) name: string, | |
@paramSchema(personAgeSchema) age: number | |
) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person { | |
@propertySchema(schemas.personAgeSchema) | |
age: number | |
@propertySchema(schemas.personNameSchema) | |
name: string | |
constructor(name: string, age: number) { | |
this.age = age | |
this.name = name | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function propertySchema(schema: Schema) { | |
return function(target: Object, key: string | symbol): void { | |
// The property | |
let val = target[key]; | |
let propertyName = String(key); | |
// Generate getter and setter | |
const getter = () => { | |
return val; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@schema(personSchema,/*validateParams:*/ true) | |
class Person { | |
age: number | |
name: string | |
constructor( | |
@paramSchema(personNameSchema) name: string, | |
@paramSchema(personAgeSchema) age: number | |
) { | |
;(this.age = age), (this.name = name) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function validateConstructorParams(target: any, args: any[]) { | |
// Retrieve all constructor parameters metadata | |
let existingConstrainedParameters: ConstrainedParameterMap = Reflect.getOwnMetadata( | |
parameterSchemaMetadataKey, | |
target, | |
"constructor" | |
); | |
// For each retrieved metadata,... | |
if (existingConstrainedParameters) { | |
const parameterIndexes = Object.keys(existingConstrainedParameters); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function schema(schema: Schema, validateParams: boolean = false) { | |
return function validateArgs(target: any) { | |
// save a reference to the original constructor | |
var original = target; | |
// wrap orginal constructor with validation behaviour | |
var f: any = function(...args) { | |
// When parameter validation is enabled... | |
if (validateParams) { | |
validateConstructorParams(target, args); // validate the parameters | |
} |
NewerOlder