Skip to content

Commit

Permalink
[fix] broken tests, and exact support for #scrub
Browse files Browse the repository at this point in the history
  • Loading branch information
calvintwr committed Oct 20, 2021
1 parent 11edbdd commit a49e180
Show file tree
Hide file tree
Showing 28 changed files with 58 additions and 39 deletions.
7 changes: 4 additions & 3 deletions dist/browser/not.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* you-are-not v1.0.4
* you-are-not v1.0.5
* (c) 2020-2021 calvintwr
* Release under MIT license.
*/
Expand Down Expand Up @@ -477,9 +477,10 @@ var You = {
return this.are(expect, got, name, note);
} // scrub is a shorthand of #checkObject
,
scrub: function scrub(name, expectObject, gotObject) {
scrub: function scrub(name, expectObject, gotObject, exact) {
return this.checkObject(name, expectObject, gotObject, {
returnPayload: true
returnPayload: true,
exact: exact
});
}
};
Expand Down
4 changes: 2 additions & 2 deletions dist/browser/not.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "you-are-not",
"version": "1.0.4",
"version": "1.0.5",
"description": "Runtime type-checking written in TypeScript -- Not is a minimal, blazing fast, intuitive, API-centric, and customisable API payload sanitiser/type-checking/validation/error handing and messaging helper -- all in a small and neat pack.",
"main": "index.cjs",
"exports": {
Expand Down
33 changes: 25 additions & 8 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,11 @@ const schema = {
let sanitised = Not.scrub(
'payloadWithTypeError', // give your payload a name
schema,
payload
payload,
{ exact: true } // use exact: true if you need the payload to match the schema 100%, else, additional properties will be removed without throwing errors.
)
```
**API error resNot throws an actionable error message:**
**Not throws an actionable error message ready for sending back to the requestor:**
```
TypeError (NotTS): Wrong types provided. See `trace`.
... stack trace ...
Expand All @@ -83,13 +84,13 @@ If you are using express or fastify, thrown errors can be seamlessly used for pr
//express
res.status(sanitised.statusCode)
res.send({
message: `You have provided erroneous inputs. \n\nMore info:\n${sanitised.track.join('\n')}`
message: `You have provided erroneous inputs. \n\nMore info:\n${sanitised.trace.join('\n')}`
})

//fastify
reply.code(sanitised.statusCode)
reply.send({
message: `You have provided erroneous inputs. \n\nMore info:\n${sanitised.track.join('\n')}`
message: `You have provided erroneous inputs. \n\nMore info:\n${sanitised.trace.join('\n')}`
})
```
This will produce a `400` error with the follow `message` property in response body:
Expand Down Expand Up @@ -135,7 +136,7 @@ const is = Not.createIs()
const notNerfed = Not.create({ throw: false }) // creates an instance that will not throw errors.
```

Use *Not* to cuts down runtime type-checking verbiage. Instead of:
Use *Not* to cut down runtime type-checking verbiage. Instead of:

```js
if (typeof foo !== 'string' ||
Expand Down Expand Up @@ -201,8 +202,9 @@ Other custom types:
#checkObject is #scrub under the hood. Use #scrub for simplified usage (example above), and #checkObject when you want more control.

```js
Not.scrub(objectName, schema, payload, { exact: true/false} )
Not.checkObject(objectName, schema, payload, options)
Not.scrub(objectName, schema, payload, options)

Not.checkObject(objectName, schema, payload, callback/options)
```

`objectName`: (string) Name of object.
Expand All @@ -211,7 +213,22 @@ Not.checkObject(objectName, schema, payload, options)

`payload`: (object) The payload to check for.

`callback/options`: (function or object | optional). To receive a sanitised payload, set options `returnPayload` to `true` (see examples below).
`options` (#scrub): (object | optional). Define `exact: true` if you want to throw error if there are additional properties.

`callback/options` (#checkObject): (object | optional). See example below:

```js
// callback
Not.checkObject(objectName, schema, payload, (errors, payload) => { /* handle errors yourself*/ })

// options
Not.checkObject(objectName, schema, payload, {
callback: (errors, payload) => { /* handle errors yourself*/ },
returnPayload: true/false, // define if you need the payload returned. if not requires, switch to false for better performance
exact: true/false // if true, will throw errors if there are additiona properties
})
```


#### Defining Schema

Expand Down
5 changes: 3 additions & 2 deletions src/You.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ const You: YouType = {
, callback?: {
callback?: CallbackFn
, returnPayload?: boolean
, exact?: true
, exact?: boolean | undefined
} | CallbackFn

): Object | string {
Expand Down Expand Up @@ -610,9 +610,10 @@ const You: YouType = {
name: string
, expectObject: GenericObj
, gotObject: GenericObj
, exact?: true

): Object | string {
return this.checkObject(name, expectObject, gotObject, { returnPayload: true })
return this.checkObject(name, expectObject, gotObject, { returnPayload: true, exact })
}

}
Expand Down
2 changes: 2 additions & 0 deletions src/core/types/You.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ interface YouType {
, callback?: {
callback?: CallbackFn
, returnPayload?: boolean
, exact?: true
} | CallbackFn
) => Object | string

Expand All @@ -45,6 +46,7 @@ interface YouType {
name: string
, expectObject: GenericObj
, gotObject: GenericObj
, exact?: true

) => Object | string

Expand Down
Empty file added src/core/types/global.d.ts
Empty file.
2 changes: 1 addition & 1 deletion test/_are.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

import Not from '../index.js'
import Not from '../index.cjs'
import chai from 'chai'
import NotTypeError from '../dist/node/core/NotTypeError.js'
chai.should()
Expand Down
2 changes: 1 addition & 1 deletion test/_are.throw.false.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

import NotProto from '../index.js'
import NotProto from '../index.cjs'
import chai from 'chai'
import NotTypeError from '../dist/node/core/NotTypeError.js'
chai.should()
Expand Down
2 changes: 1 addition & 1 deletion test/checkObject.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

import Not from '../index.js'
import Not from '../index.cjs'
import chai from 'chai'
import NotTypeError from '../dist/node/core/NotTypeError.js'
chai.should()
Expand Down
2 changes: 1 addition & 1 deletion test/checkObject.throw.false.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

import NotProto from '../index.js'
import NotProto from '../index.cjs'
import chai from 'chai'
import NotTypeError from '../dist/node/core/NotTypeError.js'
chai.should()
Expand Down
2 changes: 1 addition & 1 deletion test/createIs.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

import Not from '../index.js'
import Not from '../index.cjs'
import chai from 'chai'
import NotTypeError from '../dist/node/core/NotTypeError.js'
chai.should()
Expand Down
2 changes: 1 addition & 1 deletion test/createIs.throw.false.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

import Not from '../index.js'
import Not from '../index.cjs'
import chai from 'chai'

chai.should()
Expand Down
2 changes: 1 addition & 1 deletion test/defineType.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

import Not from '../index.js'
import Not from '../index.cjs'
import chai from 'chai'
import NotTypeError from '../dist/node/core/NotTypeError.js'
chai.should()
Expand Down
2 changes: 1 addition & 1 deletion test/defineType.throw.false.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

import NotProto from '../index.js'
import NotProto from '../index.cjs'
import chai from 'chai'

chai.should()
Expand Down
2 changes: 1 addition & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

import Not from '../index.js'
import Not from '../index.cjs'
import NotTypeError from '../dist/node/core/NotTypeError.js'
import chai from 'chai'
chai.should()
Expand Down
4 changes: 1 addition & 3 deletions test/index.throw.false.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict'

//const Not = require('../index.js')
// ES6
import NotProto from '../index.js'
import NotProto from '../index.cjs'
const Not = NotProto.create({ throw: false })

// ES6 Shorthand
Expand Down
2 changes: 1 addition & 1 deletion test/lodge.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

import Not from '../index.js'
import Not from '../index.cjs'
import chai from 'chai'
chai.should()

Expand Down
2 changes: 1 addition & 1 deletion test/lodge.throw.false.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

import NotProto from '../index.js'
import NotProto from '../index.cjs'
import chai from 'chai'
chai.should()

Expand Down
2 changes: 1 addition & 1 deletion test/msgPOJO.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

import Not from '../index.js'
import Not from '../index.cjs'
import chai from 'chai'
import NotTypeError from '../dist/node/core/NotTypeError.js'
chai.should()
Expand Down
2 changes: 1 addition & 1 deletion test/msgPOJO.throw.false.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

import NotProto from '../index.js'
import NotProto from '../index.cjs'
import chai from 'chai'
import NotTypeError from '../dist/node/core/NotTypeError.js'
chai.should()
Expand Down
2 changes: 1 addition & 1 deletion test/prepareExpect.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

import Not from '../index.js'
import Not from '../index.cjs'
import chai from 'chai'

chai.should()
Expand Down
2 changes: 1 addition & 1 deletion test/prepareExpect.throw.false.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

import NotProto from '../index.js'
import NotProto from '../index.cjs'
import chai from 'chai'

chai.should()
Expand Down
2 changes: 1 addition & 1 deletion test/resolve.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

import Not from '../index.js'
import Not from '../index.cjs'
import chai from 'chai'
import NotTypeError from '../dist/node/core/NotTypeError.js'
chai.should()
Expand Down
2 changes: 1 addition & 1 deletion test/resolve.throw.false.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

import NotProto from '../index.js'
import NotProto from '../index.cjs'
import chai from 'chai'

chai.should()
Expand Down
2 changes: 1 addition & 1 deletion test/scrub.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

import Not from '../index.js'
import Not from '../index.cjs'
import chai from 'chai'
import NotTypeError from '../dist/node/core/NotTypeError.js'
chai.should()
Expand Down
2 changes: 1 addition & 1 deletion test/timestamp.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

import Not from '../index.js'
import Not from '../index.cjs'
import chai from 'chai'
import NotTypeError from '../dist/node/core/NotTypeError.js'
chai.should()
Expand Down
2 changes: 1 addition & 1 deletion test/verbose.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

import Not from '../index.js'
import Not from '../index.cjs'
import chai from 'chai'
import NotTypeError from '../dist/node/core/NotTypeError.js'
chai.should()
Expand Down

0 comments on commit a49e180

Please sign in to comment.