Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@property({
type: 'string',
id: true,
})
orderId?: string;
// Each order belongs to a user, indentified by its id (userId)
@belongsTo(() => User)
userId: string;
@property({
type: 'number',
})
total?: number;
@property.array(ShoppingCartItem, {required: true})
products: ShoppingCartItem[];
constructor(data?: Partial) {
super(data);
}
}
it('properly converts primitive array properties', () => {
@model()
class TestModel {
@property.array(Number)
numArr: number[];
}
const jsonSchema = modelToJsonSchema(TestModel);
expect(jsonSchema.properties).to.deepEqual({
numArr: {
type: 'array',
items: {
type: 'number',
},
},
});
expectValidJsonSchema(jsonSchema);
});
it('properly converts optional primitive array properties', () => {
@model()
class TestModel {
@property.array('number')
numArr?: number[];
}
const jsonSchema = modelToJsonSchema(TestModel);
expect(jsonSchema.properties).to.deepEqual({
numArr: {
type: 'array',
items: {
type: 'number',
},
},
});
expectValidJsonSchema(jsonSchema);
});
it('allows recursive model definition', () => {
@model()
class ReportState extends Model {
@property.array(ReportState, {})
states: ReportState[];
@property({
type: 'string',
})
benchmarkId?: string;
@property({
type: 'string',
})
color?: string;
constructor(data?: Partial) {
super(data);
}
}
it('properly converts properties with recursive arrays', () => {
@model()
class RecursiveArray {
@property.array(Array)
recArr: string[][];
}
const jsonSchema = modelToJsonSchema(RecursiveArray);
expect(jsonSchema.properties).to.eql({
recArr: {
type: 'array',
items: {
type: 'array',
},
},
});
});
it('properly converts decorated custom array type with a resolver', () => {
@model()
class Address {
@property()
city: string;
}
@model()
class TestModel {
@property.array(() => Address)
addresses: Address[];
}
const jsonSchema = modelToJsonSchema(TestModel);
expect(jsonSchema.properties).to.deepEqual({
addresses: {
type: 'array',
items: {$ref: '#/definitions/Address'},
},
});
expect(jsonSchema.definitions).to.deepEqual({
Address: {
title: 'Address',
properties: {
city: {
type: 'string',
it('creates definitions only at the root level of the schema', () => {
@model()
class CustomTypeFoo {
@property()
prop: string;
}
@model()
class CustomTypeBar {
@property.array(CustomTypeFoo)
prop: CustomTypeFoo[];
}
@model()
class TestModel {
@property()
cusBar: CustomTypeBar;
}
const jsonSchema = modelToJsonSchema(TestModel);
const schemaProps = jsonSchema.properties;
const schemaDefs = jsonSchema.definitions;
expect(schemaProps).to.deepEqual({
cusBar: {
$ref: '#/definitions/CustomTypeBar',
},
import {Entity, model, property, belongsTo} from '@loopback/repository';
import {ShoppingCartItem} from './shopping-cart-item.model';
import {User} from './user.model';
@model()
export class ShoppingCart extends Entity {
/**
* Each shopping cart belongs to a user, indentified by its id (userId)
*/
@belongsTo(() => User)
userId: string;
/**
* Items in the shopping cart
*/
@property.array(ShoppingCartItem)
items?: ShoppingCartItem[];
constructor(data?: Partial) {
super(data);
}
}
@model()
export class Character extends Entity {
@property({
type: 'string',
id: true,
required: true,
})
email?: string;
@property({
type: 'string',
required: true,
})
password: string;
@property.array(String)
permissions: String[];
@property({
type: 'string',
required: true,
})
name: string;
@property({
type: 'number',
default: 1,
})
level?: number;
@property({
type: 'number',
import { model, property, AnyType } from '@loopback/repository';
@model({ name: 'ResponseMessage' })
//the red squiggly is OK here :)
export class ResponseMessage {
constructor(data?: Partial) {
if (data != null && typeof data === 'object') {
Object.assign(this, data);
}
}
@property({ name: 'message', required: true })
message: string = 'OK';
@property.array(AnyType, { name: 'objectlist', required: false })
objectlist: any[] = [];
@property({ name: 'statusCode', required: true })
statusCode: string = '200';
}