mockaton 8.12.2 → 8.12.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/utils/jwt.js CHANGED
@@ -1,7 +1,7 @@
1
- export function jwtCookie(cookieName, payload) {
1
+ export function jwtCookie(cookieName, payload, path = '/') {
2
2
  return [
3
3
  `${cookieName}=${jwt(payload)}`,
4
- 'Path=/',
4
+ `Path=${path}`,
5
5
  'SameSite=strict'
6
6
  ].join(';')
7
7
  }
@@ -0,0 +1,8 @@
1
+ export function validate(obj, shape) {
2
+ for (const [field, value] of Object.entries(obj))
3
+ if (!shape[field](value))
4
+ throw new TypeError(`${field}=${JSON.stringify(value)} is invalid`)
5
+ }
6
+
7
+ export const is = ctor => val => val.constructor === ctor
8
+ export const optional = tester => val => !val || tester(val)
@@ -0,0 +1,47 @@
1
+ import { describe, it } from 'node:test'
2
+ import { doesNotThrow, throws } from 'node:assert/strict'
3
+ import { validate, is, optional } from './validate.js'
4
+
5
+
6
+ describe('validate', () => {
7
+ describe('optional', () => {
8
+ it('accepts undefined', () =>
9
+ doesNotThrow(() =>
10
+ validate({}, { field: optional(Number.isInteger) })))
11
+
12
+ it('accepts falsy value regardless of type', () =>
13
+ doesNotThrow(() =>
14
+ validate({ field: 0 }, { field: optional(Array.isArray) })))
15
+
16
+ it('accepts when tester func returns truthy', () =>
17
+ doesNotThrow(() =>
18
+ validate({ field: [] }, { field: optional(Array.isArray) })))
19
+
20
+ it('rejects when tester func returns falsy', () =>
21
+ throws(() =>
22
+ validate({ field: 1 }, { field: optional(Array.isArray) }),
23
+ /field=1 is invalid/))
24
+ })
25
+
26
+ describe('is', () => {
27
+ it('rejects mismatched type', () =>
28
+ throws(() =>
29
+ validate({ field: 1 }, { field: is(String) }),
30
+ /field=1 is invalid/))
31
+
32
+ it('accepts matched type', () =>
33
+ doesNotThrow(() =>
34
+ validate({ field: '' }, { field: is(String) })))
35
+ })
36
+
37
+ describe('custom tester func', () => {
38
+ it('rejects mismatched type', () =>
39
+ throws(() =>
40
+ validate({ field: 0.1 }, { field: Number.isInteger }),
41
+ /field=0.1 is invalid/))
42
+
43
+ it('accepts matched type', () =>
44
+ doesNotThrow(() =>
45
+ validate({ field: 1 }, { field: Number.isInteger })))
46
+ })
47
+ })