Skip to content

Commit

Permalink
v27.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
rjrodger committed Jul 12, 2024
1 parent 37f936d commit 2530a35
Show file tree
Hide file tree
Showing 11 changed files with 1,640 additions and 1,374 deletions.
2,782 changes: 1,434 additions & 1,348 deletions coverage/lcov.info

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions dist/lib/make_entity.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ declare class Entity implements Record<string, any> {
};
constructor(canon: any, seneca: any, options: any);
make$(...args: any[]): any;
valid$(opts?: {
throws?: boolean;
errors?: boolean;
entmsg?: any;
}): boolean | any[];
/** Save the entity.
* param {object} [data] - Subset of entity field values.
* param {callback~save$} done - Callback function providing saved entity.
Expand Down
68 changes: 59 additions & 9 deletions dist/lib/make_entity.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/lib/make_entity.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/valid.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { Entity } from './lib/make_entity';
declare function buildValidation(seneca: any, entity: Entity, options: any): void;
declare function buildValidation(_seneca: any, entity: Entity, options: any): void;
export { buildValidation };
7 changes: 3 additions & 4 deletions dist/valid.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/valid.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "seneca-entity",
"main": "dist/entity.js",
"version": "27.1.1",
"version": "27.2.0",
"type": "commonjs",
"types": "dist/entity.d.ts",
"description": "Entity plugin for seneca",
Expand Down Expand Up @@ -50,6 +50,7 @@
"node": ">=16"
},
"dependencies": {
"gubu": "^8.2.1",
"seneca-mem-store": "^9.2.0"
},
"peerDependencies": {
Expand All @@ -65,8 +66,5 @@
"prettier": "^3.3.2",
"seneca-msg-test": "^4.1.0",
"typescript": "^5.5.3"
},
"overrides": {
"gubu":"8.0.2"
}
}
65 changes: 64 additions & 1 deletion src/lib/make_entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function strictCanon(ent: Entity, entmsg: any) {
'-/-/-' !== ent.entity$ // template entity
) {
let entDefined =
options.ent[ent.entity$] || options.ent[ent.entity$.replace(/-\//g, '')]
options.ent[ent.entity$] || options.ent[ent.entity$.replace(/[-\/]/g, '')]
// console.log('STRICT', Object.keys(options.ent), entDefined, ent.entity$)
if (!entDefined) {
const si = ent.private$.get_instance()
Expand Down Expand Up @@ -227,6 +227,65 @@ class Entity implements Record<string, any> {
return entity
}

valid$(opts?: {
throws?: boolean
errors?: boolean
entmsg?: any
}): boolean | any[] {
const self = this
const throws = opts?.throws

const si = self.private$.get_instance()
const entmsg = self.private$.makeEntMsg(self, opts?.entmsg || {})

const entityTemplate = (si.private$ as any).entity
const canonRouter = entityTemplate.canonRouter$

if (canonRouter) {
const canonOps = canonRouter.find(entmsg)

if (canonOps) {
if (canonOps.shape) {
let odata = entmsg.ent.data$(false)

let sctx: any = {}
if (null == odata.id) {
sctx.skip = { keys: ['id'] }
} else {
// TODO: handle merge off case
sctx.skip = { depth: 1 }
}

let skip$ = entmsg.q?.skip$
if (skip$) {
skip$ = 'string' === typeof skip$ ? skip$.split(',') : skip$
skip$ = Array.isArray(skip$) ? skip$.map((f: any) => '' + f) : []
sctx.skip = sctx.skip || {}
sctx.skip.keys = sctx.skip.keys || []
sctx.skip.keys = sctx.skip.keys.concat(skip$)
}

if (opts?.errors || !throws) {
sctx.err = []
}

let vdata = canonOps.shape(odata, sctx)

if (sctx.err && 0 < sctx.err.length) {
return true === opts?.errors ? sctx.err : false
}
entmsg.ent.data$(vdata)
}
}
}

if (opts?.errors) {
return []
}

return true
}

/** Save the entity.
* param {object} [data] - Subset of entity field values.
* param {callback~save$} done - Callback function providing saved entity.
Expand All @@ -239,6 +298,9 @@ class Entity implements Record<string, any> {
const done$ = prepareCmd(self, data, entmsg, done)
entmsg = self.private$.makeEntMsg(self, entmsg)

self.valid$({ throws: true, entmsg })

/*
const entityTemplate = (si.private$ as any).entity
const canonRouter = entityTemplate.canonRouter$
Expand Down Expand Up @@ -270,6 +332,7 @@ class Entity implements Record<string, any> {
}
}
}
*/

const promise = self.private$.promise && !done$

Expand Down
8 changes: 3 additions & 5 deletions src/valid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Gubu } from 'gubu'
import { Entity, MakeEntity } from './lib/make_entity'


function buildValidation(seneca: any, entity: Entity, options: any) {
function buildValidation(_seneca: any, entity: Entity, options: any) {
// console.log('VALID OPTS')
// console.dir(options, { depth: null })

Expand All @@ -23,17 +23,15 @@ function buildValidation(seneca: any, entity: Entity, options: any) {
const spec = canonMap[cstr]

let shape
let vopts = { prefix: cstr }
let vopts = { name: 'Entity: ' + cstr }
if (spec.valid_json) {
shape = Gubu.build(spec.valid_json) // , vopts) // , { prefix: cstr })
shape = Gubu.build(spec.valid_json, vopts)
}
else if (spec.valid) {
// let valid = ('function' === typeof spec.valid && !Gubu.isShape(spec.valid)) ?
let valid = ('function' === typeof spec.valid && !spec.valid.gubu) ?
spec.valid() : spec.valid

// console.log('VALID', valid)

shape = Gubu(valid, vopts)
// console.log('SHAPE', shape.spec())
}
Expand Down
67 changes: 67 additions & 0 deletions test/valid.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ describe('valid', function () {
await seneca.entity('foo').save$({ a: 'A' })
expect(false).toEqual(true)
} catch (e) {
// console.log(e)
expect(e.props).toEqual([
{ path: 'a', what: 'type', type: 'number', value: 'A' },
])
Expand Down Expand Up @@ -146,4 +147,70 @@ describe('valid', function () {
})
}
})

test('method', async function () {
const seneca = Seneca()
.test()
.use(Entity, {
ent: {
'-/-/foo': {},
'qaz/zed/bar': {
valid_json: {
a: 'Number',
x: 2,
},
},
'wax/-/-': {
valid_json: {
b: 'Number',
},
},
},
})

const foo0 = seneca.entity('foo')
expect(foo0.valid$()).toEqual(true)

const nim0 = seneca.entity('nim')
expect(nim0.valid$()).toEqual(true)

const bar0 = seneca.entity('qaz/zed/bar').data$({ a: 1 })
expect(bar0.valid$()).toEqual(true)
expect(bar0.data$(false)).toEqual({ a: 1, x: 2 })

const wax0 = seneca.entity('wax/wex/wix').data$({ b: 3 })
expect(wax0.valid$()).toEqual(true)
expect(wax0.data$(false)).toEqual({ b: 3 })

const bar1 = seneca.entity('qaz/zed/bar').data$({ a: 'A' })
expect(bar1.valid$()).toEqual(false)
expect(bar1.data$(false)).toEqual({ a: 'A' })

try {
bar1.valid$({ throws: true })
expect(true).equal(false)
} catch (e) {
// console.log(e)
expect(e.props).toEqual([
{ path: 'a', what: 'type', type: 'number', value: 'A' },
])
}

const errs0 = bar1.valid$({ errors: true })
// console.log(errs0)
expect(errs0[0]).toMatchObject({
key: 'a',
type: 'number',
value: 'A',
path: 'a',
why: 'type',
check: 'none',
args: {},
mark: 1050,
text:
'Validation failed for property "a" with string "A" because ' +
'the string is not of type number.',
use: {},
})
})
})

0 comments on commit 2530a35

Please sign in to comment.