@rawmodel/schema
Version:
JSON Schema utils for RawModel.
210 lines • 6.1 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@rawmodel/core");
const spec_1 = require("@hayspec/spec");
const parsers_1 = require("@rawmodel/parsers");
const validators_1 = require("@rawmodel/validators");
const __1 = require("../../..");
const spec = new spec_1.Spec();
spec.test('generates empty model class', (ctx) => {
const Klass = __1.createModelClass({});
ctx.true(new Klass() instanceof core_1.Model);
});
spec.test('generates model with properties', (ctx) => {
const Klass = __1.createModelClass({
props: [
{ name: 'firstName' },
{ name: 'lastName' },
],
});
const data = {
firstName: 'John',
lastName: 'Smith',
};
const model = new Klass(data);
ctx.deepEqual(model.serialize(), data);
});
spec.test('supports property custom getter', (ctx) => {
const Klass = __1.createModelClass({
getters: {
foo() { return () => 'foo'; },
},
props: [
{
name: 'firstName',
getter: 'foo',
},
],
});
const model = new Klass();
ctx.deepEqual(model.serialize(), {
firstName: 'foo',
});
});
spec.test('supports property custom setter', (ctx) => {
const Klass = __1.createModelClass({
setters: {
foo() { return () => 'foo'; },
},
props: [
{
name: 'firstName',
setter: 'foo',
},
],
});
const model = new Klass({
firstName: 'bar',
});
ctx.deepEqual(model.serialize(), {
firstName: 'foo',
});
});
spec.test('supports property default value', (ctx) => {
const Klass = __1.createModelClass({
defaultValues: {
smithName() { return 'Smith'; },
},
props: [
{
name: 'firstName',
defaultValue: 'John',
},
{
name: 'lastName',
defaultValue: 'smithName',
},
],
});
const model = new Klass();
ctx.deepEqual(model.serialize(), {
firstName: 'John',
lastName: 'Smith',
});
});
spec.test('supports property fake value', (ctx) => {
const Klass = __1.createModelClass({
fakeValues: {
smithName() { return 'Smith'; },
},
props: [
{
name: 'firstName',
fakeValue: 'John',
},
{
name: 'lastName',
fakeValue: 'smithName',
},
],
});
const model = new Klass().fake();
ctx.deepEqual(model.serialize(), {
firstName: 'John',
lastName: 'Smith',
});
});
spec.test('supports property empty value', (ctx) => {
const Klass = __1.createModelClass({
emptyValues: {
emptyString() { return ''; },
},
props: [
{
name: 'firstName',
emptyValue: 'none',
},
{
name: 'lastName',
emptyValue: 'emptyString',
},
],
});
const model = new Klass();
ctx.deepEqual(model.serialize(), {
firstName: 'none',
lastName: '',
});
});
spec.test('supports property parsers', (ctx) => {
const Klass = __1.createModelClass({
parsers: {
string: parsers_1.stringParser,
},
props: [
{
name: 'firstName',
parser: {
array: true,
resolver: 'string',
},
},
],
});
const model = new Klass({
firstName: 100,
});
ctx.deepEqual(model.serialize(), {
firstName: ['100'],
});
});
spec.test('supports property validators', (ctx) => __awaiter(void 0, void 0, void 0, function* () {
const Klass = __1.createModelClass({
validators: {
stringLength: validators_1.stringLengthValidator,
},
props: [
{
name: 'firstName',
validators: [
{
resolver: 'stringLength',
code: 400,
options: { min: 5 },
},
],
},
],
});
const model = new Klass({
firstName: '123',
});
yield ctx.throws(() => model.validate());
ctx.deepEqual(model.collectErrors(), [
{ path: ['firstName'], code: 400 },
]);
}));
spec.test('supports property handlers', (ctx) => __awaiter(void 0, void 0, void 0, function* () {
const Klass = __1.createModelClass({
handlers: {
generalError() { return (e) => true; },
},
props: [
{
name: 'firstName',
handlers: [
{
resolver: 'generalError',
code: 400,
},
],
},
],
});
const model = new Klass();
yield ctx.throws(() => model.handle(new Error(), { quiet: false }));
ctx.deepEqual(model.collectErrors(), [
{ path: ['firstName'], code: 400 },
]);
}));
exports.default = spec;
//# sourceMappingURL=create-model-method.test.js.map