Skip to content

Commit eadb119

Browse files
committed
Add multi-spec
1 parent 8764d9c commit eadb119

18 files changed

+37079
-11118
lines changed

lib/core/package-lock.json

Lines changed: 1357 additions & 1019 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"@json-spec/testcheck": "^0.1.6"
1313
},
1414
"devDependencies": {
15-
"jest": "^25.1.0"
15+
"jest": "^26.6.3"
1616
},
1717
"gitHead": "3a6df1d730449208b7db3816fb1db26cd672228c"
1818
}

lib/core/src/__tests__/spec.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ describe('explains', () => {
185185
address: "Tokyo City",
186186
score: 10,
187187
})).toBe(true);
188+
188189
})
189190

190191
test('object anyOf', () => {
@@ -223,3 +224,28 @@ describe('explains', () => {
223224
})).toBe(true);
224225
})
225226
})
227+
228+
describe('multiSpec', () => {
229+
test('multiSpec', () => {
230+
const spec = s.multi(o => o.nyuharai_kbn, {
231+
1: s.object({
232+
required: {
233+
nyuharai_kbn: 1,
234+
irainin_or_contract_no: s.spec(/\w+/)
235+
}
236+
}),
237+
2: s.object({
238+
required: {
239+
nyuharai_kbn: 2,
240+
irainin_or_contract_no: s.spec(/\d{20}[ ]{28}/)
241+
}
242+
})
243+
});
244+
const value = {
245+
nyuharai_kbn: 2,
246+
irainin_or_contract_no: '01'
247+
};
248+
console.log(gen.sample(s.gen(spec), 10));
249+
expect(s.conform(spec, value)).toBe(s.INVALID);
250+
})
251+
})

lib/core/src/base.js

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
const assert = require('assert');
21
const g = require('./gen');
32

43
class Invalid {
@@ -38,9 +37,10 @@ class Spec {
3837
}
3938

4039
/**
41-
* @param {any} val
40+
* @param {any} val
41+
* @param {Object} settings
4242
*/
43-
conform(val) {
43+
conform(val, settings) {
4444
throw new Error('Not Implemented');
4545
}
4646

@@ -480,6 +480,39 @@ class NullableSpec extends Spec {
480480
}
481481
}
482482

483+
class MultiSpec extends Spec {
484+
constructor(dispatchFn, forms, gfn) {
485+
super(gfn);
486+
this.dispatchFn = dispatchFn;
487+
this.forms = forms;
488+
}
489+
490+
conform(x) {
491+
const v = this.dispatchFn(x);
492+
const spec = this.forms[v];
493+
if (spec == null) return null;
494+
return spec.conform(x);
495+
}
496+
497+
gen(overrides, path, rmap) {
498+
if (this.gfn) {
499+
return this.gfn();
500+
} else {
501+
return g.oneOf.apply(null, Object.values(this.forms).map(spec =>
502+
gensub(spec, overrides, path, rmap, spec)));
503+
}
504+
}
505+
506+
explain(path, via, in_, x) {
507+
if (this.conform(x) === INVALID) {
508+
const v = this.dispatchFn(x);
509+
return this.forms[v].explain(path, via, in_, x);
510+
}
511+
return null;
512+
}
513+
514+
}
515+
483516
/**
484517
*
485518
*/
@@ -518,7 +551,9 @@ function or(...preds) {
518551
}
519552

520553
function array(pred, opts={}) {
521-
assert.ok(pred != null);
554+
if (pred == null) {
555+
throw new Error('pred must not be null');
556+
}
522557
return new ArraySpec(pred.toString(), pred, opts);
523558
}
524559

@@ -541,13 +576,17 @@ function nullable(pred) {
541576
return new NullableSpec(pred, pred, null);
542577
}
543578

544-
function conform(spec, x) {
545-
return specize(spec).conform(x);
579+
function multi(dispatchFn, specMap) {
580+
return new MultiSpec(dispatchFn, specMap);
581+
}
582+
583+
function conform(spec, x, settings = {}) {
584+
return specize(spec).conform(x, settings);
546585
}
547586

548-
function isValid(spec, x) {
587+
function isValid(spec, x, settings = {}) {
549588
const specized = specize(spec);
550-
return INVALID !== conform(specized, x);
589+
return INVALID !== conform(specized, x, settings);
551590
}
552591

553592
function gen(spec, overrides) {
@@ -572,6 +611,7 @@ module.exports = {
572611
object,
573612
tuple,
574613
nullable,
614+
multi,
575615
isValid,
576616
isSpec,
577617
specize,

0 commit comments

Comments
 (0)