UNPKG

@backland/schema

Version:

TypeScript schema declaration and validation library with static type inference

238 lines (236 loc) 6.71 kB
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } import { assert } from 'conditional-type-checks'; describe('extendObjectDefinition', () => { // afterEach(); test('works', /*#__PURE__*/_asyncToGenerator(function* () { assert(true); })); }); // import { assert, IsExact } from 'conditional-type-checks'; // // import { createType } from '../GraphType/GraphType'; // import { ObjectType } from '../ObjectType'; // import {extendObjectDefinition, InnerDef} from '../extendObjectDefinition'; // import {DescribeField} from "./Infer"; // // describe('extendObjectDefinition', () => { // afterEach(ObjectType.reset); // // test('value', async () => { // const type = createType('fooo', { // object: { // name: 'string', // age: 'int', // }, // }); // // const res = extendObjectDefinition(type).def(); // assert<IsExact<typeof res.age.type, 'int'>>(true); // assert<IsExact<typeof res.name.type, 'string'>>(true); // // expect(res).toMatchObject({ // age: { // type: 'int', // }, // name: { // type: 'string', // }, // }); // }); // // test('only', async () => { // const type = createType('fooo', { // object: { // name: 'string', // age: 'int', // }, // }); // // extendObjectDefinition(type).only('name').def(); // const res = extendObjectDefinition(type).only(['name']).def(); // // // @ts-expect-error // res.age?.type; // // assert<IsExact<typeof res.name.type, 'string'>>(true); // // expect(res).toEqual({ // name: { // type: 'string', // }, // }); // }); // // test('exclude', async () => { // const type = createType('fooo', { // object: { // name: 'string', // age: 'int', // }, // }); // // extendObjectDefinition(type).exclude('name').def(); // const res = extendObjectDefinition(type).exclude(['name']).def(); // // // @ts-expect-error // res.name?.type; // // assert<IsExact<typeof res.age.type, 'int'>>(true); // // expect(res).toEqual({ // age: { // type: 'int', // }, // }); // }); // // test('optional', async () => { // const type = createType('fooo', { // object: { // name: 'string', // age: 'int', // }, // }); // // const res = extendObjectDefinition(type).optional(['name']).def(); // extendObjectDefinition(type).optional('name').def(); // // assert<IsExact<typeof res.age.type, 'int'>>(true); // assert<IsExact<typeof res.name.optional, true>>(true); // // expect(res).toEqual({ // name: { // optional: true, // type: 'string', // }, // age: { // type: 'int', // }, // }); // }); // // test('extend', async () => { // const type = createType('fooo', { // object: { // name: 'string', // age: 'int', // }, // }); // // const res = extendObjectDefinition(type) // .extendObjectDefinition({ a: '[string]' }) // .def(); // // assert<IsExact<typeof res.a.type, 'string'>>(true); // assert<IsExact<typeof res.a.list, true>>(true); // assert<IsExact<typeof res.a.optional, false>>(true); // // assert<IsExact<typeof res.age.type, 'int'>>(true); // assert<IsExact<typeof res.name.type, 'string'>>(true); // // expect(res).toEqual({ // a: { // list: true, // // type: 'string', // }, // age: { // type: 'int', // }, // name: { // type: 'string', // }, // }); // }); // // test('chain', async () => { // const type = createType('fooo', { // object: { // name: 'string?', // age: 'int', // }, // }); // // const res = extendObjectDefinition(type) // .only('name') // .extendObjectDefinition({ a: '[string]', x: '[int]?' }) // .exclude('x') // .optional('a') // .required('name') // .def(); // // assert<IsExact<typeof res.a.type, 'string'>>(true); // assert<IsExact<typeof res.a.list, true>>(true); // assert<IsExact<typeof res.a.optional, true>>(true); // // // @ts-expect-error // assert<IsExact<typeof res.age.type, 'int'>>(true); // // // @ts-expect-error // assert<IsExact<typeof res.x.type, 'int'>>(true); // // assert<IsExact<typeof res.name.optional, false>>(true); // assert<IsExact<typeof res.name.type, 'string'>>(true); // // expect(res).toEqual({ // a: { // list: true, // optional: true, // type: 'string', // }, // name: { // type: 'string', // }, // }); // }); // // test('deep def', async () => { // const t2 = createType('baz', { // object: { // name: 'string?', // age: 'int', // }, // }); // const t1 = createType('bar', t2); // const type = createType('fooo', t1); // // const res = extendObjectDefinition(type) // .only('name') // .extendObjectDefinition({ a: '[string]', x: '[int]?' }) // .exclude('x') // .optional('a') // .required('name') // .graphType('myClone123').definition.def; // // assert<IsExact<typeof res.a.type, 'string'>>(true); // assert<IsExact<typeof res.a.list, true>>(true); // assert<IsExact<typeof res.a.optional, true>>(true); // // // @ts-expect-error // assert<IsExact<typeof res.age.type, 'int'>>(true); // // // @ts-expect-error // assert<IsExact<typeof res.x.type, 'int'>>(true); // // assert<IsExact<typeof res.name.optional, false>>(true); // assert<IsExact<typeof res.name.type, 'string'>>(true); // // expect(res).toEqual({ // __dschm__: expect.objectContaining({ // def: { // id: 'myClone123', // }, // }), // a: { // list: true, // optional: true, // type: 'string', // }, // name: { // type: 'string', // }, // }); // }); // }); //# sourceMappingURL=extendDefinition.spec.js.map