Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('uses title from model metadata instead of model name', () => {
@model({title: 'MyCustomer'})
class Customer {}
const schema = modelToJsonSchema(Customer, {
// trigger build of a custom title
partial: true,
});
expect(schema.title).to.equal('MyCustomerPartial');
});
describe('Validation at REST level', () => {
let app: RestApplication;
let client: Client;
@model()
class Product {
@property()
id: number;
@property({required: true})
name: string;
// NOTE(rfeng): We have to add `type: 'string'` to `@property` as
// `string | null` removes TypeScript design:type reflection
@property({required: false, type: 'string', jsonSchema: {nullable: true}})
description?: string | null;
@property({required: true, jsonSchema: {range: [0, 100]}})
price: number;
constructor(data: Partial) {
// Copyright IBM Corp. 2018,2019. All Rights Reserved.
// Node module: @loopback/example-todo-list
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
import {belongsTo, Entity, model, property} from '@loopback/repository';
import {TodoList, TodoListWithRelations} from './todo-list.model';
@model()
export class TodoListImage extends Entity {
@property({
type: 'number',
id: true,
})
id: number;
@belongsTo(() => TodoList)
todoListId: number;
@property({
required: true,
})
// Ideally we would use Buffer type here, but
// that is not supported yet.
// see https://github.com/strongloop/loopback-next/issues/1742
it('respects model setting "strict: false"', () => {
@model({settings: {strict: false}})
class FreeFormModel {}
const schema = modelToJsonSchema(FreeFormModel);
expect(schema).to.containDeep({
additionalProperties: true,
});
});
});
it('properly converts decorated custom array type properties', () => {
@model()
class CustomType {
@property()
prop: string;
}
@model()
class TestModel {
@property.array(CustomType)
cusType: CustomType[];
}
const jsonSchema = modelToJsonSchema(TestModel);
expect(jsonSchema.properties).to.deepEqual({
cusType: {
type: 'array',
items: {$ref: '#/definitions/CustomType'},
it('gets cached JSON schema with relation links if one exists', () => {
@model()
class Product extends Entity {
@property({id: true})
id: number;
@belongsTo(() => Category)
categoryId: number;
}
@model()
class Category extends Entity {
@property({id: true})
id: number;
@hasMany(() => Product)
products?: Product[];
}
const cachedSchema: JsonSchema = {
definitions: {
ProductWithRelations: {
title: 'ProductWithRelations',
properties: {
id: {type: 'number'},
categoryId: {type: 'number'},
category: {$ref: '#/definitions/CategoryWithRelations'},
describe('CrudRestController for a simple Product model', () => {
@model()
class Product extends Entity {
@property({id: true})
id: number;
@property({required: true})
name: string;
@property()
description?: string;
constructor(data: Partial) {
super(data);
}
}
let app: RestApplication;
import {Entity, model, property} from '@loopback/repository';
@model()
export class Role extends Entity {
@property({
type: 'string',
id: true,
})
id?: string;
@property({
type: 'string',
required: true,
})
description: string;
constructor(data?: Partial) {
super(data);
}
import { Entity, model, property } from '@loopback/repository';
@model()
export class Item extends Entity {
@property({
type: 'number',
id: true,
required: true,
})
_id: number;
@property({
type: 'number',
})
ankamaId?: number;
@property({
type: 'string',
})
import {
CredentialsRequestBody,
UserProfileSchema,
} from './specs/user-controller.specs';
import {Credentials} from '../repositories/user.repository';
import {PasswordHasher} from '../services/hash.password.bcryptjs';
import {
TokenServiceBindings,
PasswordHasherBindings,
UserServiceBindings,
} from '../keys';
import _ from 'lodash';
import {OPERATION_SECURITY_SPEC} from '../utils/security-spec';
@model()
export class NewUserRequest extends User {
@property({
type: 'string',
required: true,
})
password: string;
}
export class UserController {
constructor(
@repository(UserRepository) public userRepository: UserRepository,
@inject('services.RecommenderService')
public recommender: RecommenderService,
@inject(PasswordHasherBindings.PASSWORD_HASHER)
public passwordHasher: PasswordHasher,
@inject(TokenServiceBindings.TOKEN_SERVICE)