Skip to content

Commit

Permalink
feat: Implement notnull Attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
moajo committed Jan 28, 2018
1 parent 2356c13 commit ea91e7a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
6 changes: 6 additions & 0 deletions src/Core/Attribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,9 @@ export class LazyAttribute<T = any> extends AttributeBase<T, () => Nullable<T>,

private _valuate(raw: any): () => Nullable<T> {
if (raw === null) {
if (this.declaration.notNull) {
throw new Error("this Attribute must not be null");
}
return () => null;
}
const v = this.converter.convert(raw, this, this.converterContext);
Expand Down Expand Up @@ -511,6 +514,9 @@ export class StandardAttribute<T = any> extends AttributeBase<T, T, IStandardAtt

private _valuate(raw: any): null | T {
if (raw === null) {
if (this.declaration.notNull) {
throw new Error("this Attribute must not be null");
}
return null;
}
const v = this.converter.convert(raw, this, this.converterContext);
Expand Down
14 changes: 8 additions & 6 deletions src/Interface/IAttributeDeclaration.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import { Name } from "../Tool/Types";
import { IConverterDeclaration, ILazyConverterDeclaration, IStandardConverterDeclaration } from "./IAttributeConverterDeclaration";

export interface IAttributeDeclarationBase {
default: any;
notNull?: boolean;
[parameters: string]: any;
}

/**
* interface for attribute declaration
*/
export interface IStandardAttributeDeclaration<T = any> {
export interface IStandardAttributeDeclaration<T = any> extends IAttributeDeclarationBase {
converter: Name | IStandardConverterDeclaration<T>;
default: any;
[parameters: string]: any;
}

/**
* interface for lazy attribute declaration
*/
export interface ILazyAttributeDeclaration<T = any> {
export interface ILazyAttributeDeclaration<T = any> extends IAttributeDeclarationBase {
converter: Name | ILazyConverterDeclaration<T>;
default: any;
[parameters: string]: any;
}

export type IAttributeDeclaration<T= any> = IStandardAttributeDeclaration<T> | ILazyAttributeDeclaration<T>;

0 comments on commit ea91e7a

Please sign in to comment.