Skip to content

Commit

Permalink
Fix parsing some schemas with refine
Browse files Browse the repository at this point in the history
  • Loading branch information
DZakh committed Jul 22, 2024
1 parent 77a88c0 commit 315a5ee
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 87 deletions.
4 changes: 2 additions & 2 deletions packages/tests/src/core/S_refine_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ test("Compiled parse code snapshot for simple object with refine", t => {
t->U.assertCompiledCode(
~schema,
~op=#Parse,
`i=>{if(!i||i.constructor!==Object){e[3](i)}let v0=i["foo"],v1=i["bar"],v2={"foo":v0,"bar":v1,};if(typeof v0!=="string"){e[0](v0)}if(typeof v1!=="boolean"){e[1](v1)}e[2](v2);return v2}`,
`i=>{if(!i||i.constructor!==Object){e[3](i)}let v0=i["foo"],v1=i["bar"],v2;if(typeof v0!=="string"){e[0](v0)}if(typeof v1!=="boolean"){e[1](v1)}v2={"foo":v0,"bar":v1,};e[2](v2);return v2}`,
)
})

Expand All @@ -95,7 +95,7 @@ module Issue79 = {
t->U.assertCompiledCode(
~schema,
~op=#Parse,
`i=>{if(!i||i.constructor!==Object){e[2](i)}let v0=i["myField"],v2,v3=v2;if(v0!==void 0&&(v0!==null&&(typeof v0!=="string"))){e[0](v0)}if(v0!==void 0){let v1;if(v0!==null){v1=v0}else{v1=void 0}v2=v1}e[1](v3);return v3}`,
`i=>{if(!i||i.constructor!==Object){e[2](i)}let v0=i["myField"],v2,v3;if(v0!==void 0&&(v0!==null&&(typeof v0!=="string"))){e[0](v0)}if(v0!==void 0){let v1;if(v0!==null){v1=v0}else{v1=void 0}v2=v1}v3=v2;e[1](v3);return v3}`,
)

t->Assert.deepEqual(jsonString->S.parseJsonStringWith(schema), Ok(Some("test")), ())
Expand Down
82 changes: 45 additions & 37 deletions src/S_Core.bs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1027,23 +1027,31 @@ function containerName() {
function internalRefine(schema, refiner) {
return make(schema.n, schema.r, schema.m, (function (b, input, selfSchema, path) {
return transform(b, schema.p(b, input, schema, path), (function (b, input) {
var rCode = refiner(b, input, selfSchema, path);
var input$1;
if (b.c === "" && input.v !== undefined) {
input$1 = input;
} else {
var scopedInput = allocateVal(b);
b.c = b.c + set(b, scopedInput, input) + ";";
input$1 = scopedInput;
}
var rCode = refiner(b, $$var(b, input$1), selfSchema, path);
b.c = b.c + rCode;
return input;
return input$1;
}));
}), (function (b, input, selfSchema, path) {
var input$1 = transform(b, input, (function (b, input) {
b.c = b.c + refiner(b, input, selfSchema, path);
b.c = b.c + refiner(b, $$var(b, input), selfSchema, path);
return input;
}));
return schema.s(b, input$1, schema, path);
}), schema.f);
}

function refine(schema, refiner) {
return internalRefine(schema, (function (b, input, selfSchema, path) {
return internalRefine(schema, (function (b, inputVar, selfSchema, path) {
var value = refiner(effectCtx(b, selfSchema, path));
return "e[" + (b.g.e.push(value) - 1) + "](" + $$var(b, input) + ");";
return "e[" + (b.g.e.push(value) - 1) + "](" + inputVar + ");";
}));
}

Expand Down Expand Up @@ -2577,8 +2585,8 @@ function intMin(schema, minValue, maybeMessage) {
value: minValue
},
message: message
}, (function (b, input, param, path) {
return "if(" + $$var(b, input) + "<" + ("e[" + (b.g.e.push(minValue) - 1) + "]") + "){" + fail(b, message, path) + "}";
}, (function (b, inputVar, param, path) {
return "if(" + inputVar + "<" + ("e[" + (b.g.e.push(minValue) - 1) + "]") + "){" + fail(b, message, path) + "}";
}));
}

Expand All @@ -2590,8 +2598,8 @@ function intMax(schema, maxValue, maybeMessage) {
value: maxValue
},
message: message
}, (function (b, input, param, path) {
return "if(" + $$var(b, input) + ">" + ("e[" + (b.g.e.push(maxValue) - 1) + "]") + "){" + fail(b, message, path) + "}";
}, (function (b, inputVar, param, path) {
return "if(" + inputVar + ">" + ("e[" + (b.g.e.push(maxValue) - 1) + "]") + "){" + fail(b, message, path) + "}";
}));
}

Expand All @@ -2600,8 +2608,8 @@ function port(schema, messageOpt) {
return addRefinement(schema, metadataId$1, {
kind: "Port",
message: message
}, (function (b, input, param, path) {
return "if(" + $$var(b, input) + "<1||" + $$var(b, input) + ">65535){" + fail(b, message, path) + "}";
}, (function (b, inputVar, param, path) {
return "if(" + inputVar + "<1||" + inputVar + ">65535){" + fail(b, message, path) + "}";
}));
}

Expand All @@ -2613,8 +2621,8 @@ function floatMin(schema, minValue, maybeMessage) {
value: minValue
},
message: message
}, (function (b, input, param, path) {
return "if(" + $$var(b, input) + "<" + ("e[" + (b.g.e.push(minValue) - 1) + "]") + "){" + fail(b, message, path) + "}";
}, (function (b, inputVar, param, path) {
return "if(" + inputVar + "<" + ("e[" + (b.g.e.push(minValue) - 1) + "]") + "){" + fail(b, message, path) + "}";
}));
}

Expand All @@ -2626,8 +2634,8 @@ function floatMax(schema, maxValue, maybeMessage) {
value: maxValue
},
message: message
}, (function (b, input, param, path) {
return "if(" + $$var(b, input) + ">" + ("e[" + (b.g.e.push(maxValue) - 1) + "]") + "){" + fail(b, message, path) + "}";
}, (function (b, inputVar, param, path) {
return "if(" + inputVar + ">" + ("e[" + (b.g.e.push(maxValue) - 1) + "]") + "){" + fail(b, message, path) + "}";
}));
}

Expand All @@ -2639,8 +2647,8 @@ function arrayMinLength(schema, length, maybeMessage) {
length: length
},
message: message
}, (function (b, input, param, path) {
return "if(" + $$var(b, input) + ".length<" + ("e[" + (b.g.e.push(length) - 1) + "]") + "){" + fail(b, message, path) + "}";
}, (function (b, inputVar, param, path) {
return "if(" + inputVar + ".length<" + ("e[" + (b.g.e.push(length) - 1) + "]") + "){" + fail(b, message, path) + "}";
}));
}

Expand All @@ -2652,8 +2660,8 @@ function arrayMaxLength(schema, length, maybeMessage) {
length: length
},
message: message
}, (function (b, input, param, path) {
return "if(" + $$var(b, input) + ".length>" + ("e[" + (b.g.e.push(length) - 1) + "]") + "){" + fail(b, message, path) + "}";
}, (function (b, inputVar, param, path) {
return "if(" + inputVar + ".length>" + ("e[" + (b.g.e.push(length) - 1) + "]") + "){" + fail(b, message, path) + "}";
}));
}

Expand All @@ -2665,8 +2673,8 @@ function arrayLength(schema, length, maybeMessage) {
length: length
},
message: message
}, (function (b, input, param, path) {
return "if(" + $$var(b, input) + ".length!==" + ("e[" + (b.g.e.push(length) - 1) + "]") + "){" + fail(b, message, path) + "}";
}, (function (b, inputVar, param, path) {
return "if(" + inputVar + ".length!==" + ("e[" + (b.g.e.push(length) - 1) + "]") + "){" + fail(b, message, path) + "}";
}));
}

Expand All @@ -2678,8 +2686,8 @@ function stringMinLength(schema, length, maybeMessage) {
length: length
},
message: message
}, (function (b, input, param, path) {
return "if(" + $$var(b, input) + ".length<" + ("e[" + (b.g.e.push(length) - 1) + "]") + "){" + fail(b, message, path) + "}";
}, (function (b, inputVar, param, path) {
return "if(" + inputVar + ".length<" + ("e[" + (b.g.e.push(length) - 1) + "]") + "){" + fail(b, message, path) + "}";
}));
}

Expand All @@ -2691,8 +2699,8 @@ function stringMaxLength(schema, length, maybeMessage) {
length: length
},
message: message
}, (function (b, input, param, path) {
return "if(" + $$var(b, input) + ".length>" + ("e[" + (b.g.e.push(length) - 1) + "]") + "){" + fail(b, message, path) + "}";
}, (function (b, inputVar, param, path) {
return "if(" + inputVar + ".length>" + ("e[" + (b.g.e.push(length) - 1) + "]") + "){" + fail(b, message, path) + "}";
}));
}

Expand All @@ -2704,8 +2712,8 @@ function stringLength(schema, length, maybeMessage) {
length: length
},
message: message
}, (function (b, input, param, path) {
return "if(" + $$var(b, input) + ".length!==" + ("e[" + (b.g.e.push(length) - 1) + "]") + "){" + fail(b, message, path) + "}";
}, (function (b, inputVar, param, path) {
return "if(" + inputVar + ".length!==" + ("e[" + (b.g.e.push(length) - 1) + "]") + "){" + fail(b, message, path) + "}";
}));
}

Expand All @@ -2714,8 +2722,8 @@ function email(schema, messageOpt) {
return addRefinement(schema, metadataId, {
kind: "Email",
message: message
}, (function (b, input, param, path) {
return "if(!" + ("e[" + (b.g.e.push(emailRegex) - 1) + "]") + ".test(" + $$var(b, input) + ")){" + fail(b, message, path) + "}";
}, (function (b, inputVar, param, path) {
return "if(!" + ("e[" + (b.g.e.push(emailRegex) - 1) + "]") + ".test(" + inputVar + ")){" + fail(b, message, path) + "}";
}));
}

Expand All @@ -2724,8 +2732,8 @@ function uuid(schema, messageOpt) {
return addRefinement(schema, metadataId, {
kind: "Uuid",
message: message
}, (function (b, input, param, path) {
return "if(!" + ("e[" + (b.g.e.push(uuidRegex) - 1) + "]") + ".test(" + $$var(b, input) + ")){" + fail(b, message, path) + "}";
}, (function (b, inputVar, param, path) {
return "if(!" + ("e[" + (b.g.e.push(uuidRegex) - 1) + "]") + ".test(" + inputVar + ")){" + fail(b, message, path) + "}";
}));
}

Expand All @@ -2734,8 +2742,8 @@ function cuid(schema, messageOpt) {
return addRefinement(schema, metadataId, {
kind: "Cuid",
message: message
}, (function (b, input, param, path) {
return "if(!" + ("e[" + (b.g.e.push(cuidRegex) - 1) + "]") + ".test(" + $$var(b, input) + ")){" + fail(b, message, path) + "}";
}, (function (b, inputVar, param, path) {
return "if(!" + ("e[" + (b.g.e.push(cuidRegex) - 1) + "]") + ".test(" + inputVar + ")){" + fail(b, message, path) + "}";
}));
}

Expand All @@ -2744,8 +2752,8 @@ function url(schema, messageOpt) {
return addRefinement(schema, metadataId, {
kind: "Url",
message: message
}, (function (b, input, param, path) {
return "try{new URL(" + $$var(b, input) + ")}catch(_){" + fail(b, message, path) + "}";
}, (function (b, inputVar, param, path) {
return "try{new URL(" + inputVar + ")}catch(_){" + fail(b, message, path) + "}";
}));
}

Expand All @@ -2757,10 +2765,10 @@ function pattern(schema, re, messageOpt) {
re: re
},
message: message
}, (function (b, input, param, path) {
}, (function (b, inputVar, param, path) {
var reVal = val(b, "e[" + (b.g.e.push(re) - 1) + "]");
var reVar = $$var(b, reVal);
return reVar + ".lastIndex=0;if(!" + reVar + ".test(" + $$var(b, input) + ")){" + fail(b, message, path) + "}";
return reVar + ".lastIndex=0;if(!" + reVar + ".test(" + inputVar + ")){" + fail(b, message, path) + "}";
}));
}

Expand Down
Loading

0 comments on commit 315a5ee

Please sign in to comment.