Skip to content
This repository has been archived by the owner on Apr 13, 2022. It is now read-only.

Commit

Permalink
Merge pull request #46 from diestrin/diestrin/issue-45/convector-mode…
Browse files Browse the repository at this point in the history
…l-validate

fix(core-model): Validate to accept convector moddels
  • Loading branch information
walmon authored Feb 24, 2019
2 parents 3358133 + 6efb592 commit 6cabc2d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
6 changes: 3 additions & 3 deletions @worldsibu/convector-core-model/src/convector-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ export interface History<T> {
export abstract class ConvectorModel<T extends ConvectorModel<any>> {
public static schema<T extends ConvectorModel<any>>(
this: new (...args: any[]) => T
): yup.ObjectSchema<FlatConvectorModel<T>&{id:string}> {
): yup.ObjectSchema<FlatConvectorModel<T>&{id:string,type:string}> {
const instance = new this();

return yup.object<FlatConvectorModel<T>&{id:string}>().shape({
return yup.object<FlatConvectorModel<T>&{id:string,type:string}>().shape({
id: yup.string().required(),
key: yup.string(),
type: yup.string(),
...getPropertiesValidation(instance)
} as any);
}
Expand Down
2 changes: 1 addition & 1 deletion @worldsibu/convector-core-model/src/validate.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function Validate<T>(input: Schema<T>|{ schema: () => Schema<T>}) {
let schema = input as Schema<T>;

if ('schema' in input) {
schema = input.schema as any;
schema = input.schema();
}

return (target: any, key: string) => {
Expand Down
20 changes: 19 additions & 1 deletion examples/token/src/token.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,23 @@ import {
ConvectorModel,
ReadOnly,
Required,
Validate
Validate,
FlatConvectorModel
} from '@worldsibu/convector-core-model';

export class Complex extends ConvectorModel<Complex> {
@ReadOnly()
public readonly type = 'io.worldsibu.examples.complex';

@Required()
@Validate(yup.mixed())
public value: any;

@Required()
@Validate(yup.string())
public name: string;
}

export class Token extends ConvectorModel<Token> {
@ReadOnly()
public readonly type = 'io.worldsibu.examples.token';
Expand All @@ -31,4 +45,8 @@ export class Token extends ConvectorModel<Token> {
@Required()
@Validate(yup.string())
public symbol: string;

@Required()
@Validate(Complex)
public complex: FlatConvectorModel<Complex>;
}
22 changes: 21 additions & 1 deletion examples/token/tests/token.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,17 @@ describe('Token', () => {
name: 'Token',
symbol: 'TKN',
totalSupply: totalSupply,
balances: { [certificate]: totalSupply }
balances: { [certificate]: totalSupply },
complex: {
name: 'Test',
value: 5
}
}));

const token = await adapter.getById<Token>(tokenId);

expect(token).to.exist;
expect(token.complex.name).to.eq('Test');
expect(token.balances[certificate]).to.eq(totalSupply);
});

Expand All @@ -54,4 +59,19 @@ describe('Token', () => {

expect(token.balances[certificate]).to.eq(500000);
});

it('should fail to create a new model if any sub model is missing', async () => {
await tokenCtrl.init(new Token({
id: tokenId + 1,
name: 'Token',
symbol: 'TKN',
totalSupply: totalSupply,
balances: { [certificate]: totalSupply }
}));

console.log('Expected error in unit-test');
const token = await adapter.getById<Token>(tokenId + 1);

expect(token).to.not.exist;
});
});

0 comments on commit 6cabc2d

Please sign in to comment.