Skip to content

Commit

Permalink
non-allocation validation (#256)
Browse files Browse the repository at this point in the history
* non-allocation validation

* refactor

* refactor

* refactor

* wip

* refactor

* wip

* wip

* all

* parse

* wip

* fix errs

* errs

* bump

* changes

* errs

* fix bugs

* fixes

* fixes

* fix bugs

* fixes

* fixes

* union error message

* validate

* fix bugs

* fix bugs

* fix bugs

* fixes

* hoist

* readable code
  • Loading branch information
lucasavila00 authored Jan 24, 2025
1 parent b749034 commit 31241f3
Show file tree
Hide file tree
Showing 44 changed files with 6,600 additions and 2,050 deletions.
59 changes: 43 additions & 16 deletions e2e-tests/bug-rust-repro/src/generated/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@

import {printErrors} from '@beff/client';
import {z} from 'zod';
import validatorsMod from "./validators.js"; const { ObjectDecoder, ArrayDecoder, decodeString, decodeNumber, CodecDecoder, decodeFunction, StringWithFormatDecoder, AnyOfDecoder, AllOfDecoder, decodeBoolean, decodeAny, TupleDecoder, decodeNull, decodeNever, RegexDecoder, ConstDecoder, registerCustomFormatter, AnyOfConstsDecoder, AnyOfDiscriminatedDecoder, validators, c } = validatorsMod;
import validatorsMod from "./validators.js"; const { registerCustomFormatter, ObjectValidator, ObjectParser, ArrayParser, ArrayValidator, CodecDecoder, StringWithFormatDecoder, AnyOfValidator, AnyOfParser, AllOfValidator, AllOfParser, TupleParser, TupleValidator, RegexDecoder, ConstDecoder, AnyOfConstsDecoder, AnyOfDiscriminatedReporter, AnyOfDiscriminatedParser, AnyOfDiscriminatedValidator, validateString, validateNumber, validateFunction, validateBoolean, validateAny, validateNull, validateNever, parseIdentity, AnyOfReporter, AllOfReporter, reportString, reportNumber, reportNull, reportBoolean, reportAny, reportNever, reportFunction, ArrayReporter, ObjectReporter, TupleReporter, validators, parsers, reporters, c } = validatorsMod;
const RequiredCustomFormats = ["ValidCurrency"];
const buildParsersInput = {
const buildValidatorsInput = {
"A": validators.A
};
const buildParsersInput = {
"A": parsers.A
};
const buildReportersInput = {
"A": reporters.A
};



Expand All @@ -30,30 +36,50 @@ function buildParsers(args) {

let decoders = {};

Object.keys(buildParsersInput).forEach((k) => {
Object.keys(buildValidatorsInput).forEach((k) => {

let v = buildParsersInput[k];
let v = buildValidatorsInput[k];
const validate = (input, options) => {
const disallowExtraProperties = options?.disallowExtraProperties ?? false;
const ctx = { disallowExtraProperties };
const ok = v(ctx, input);
if (typeof ok !== "boolean") {
throw new Error("INTERNAL ERROR: Expected boolean");
}
return ok;
};
const safeParse = (input, options) => {
const validatorCtx = {
disallowExtraProperties: options?.disallowExtraProperties ?? false,
};
const new_value = v(validatorCtx, input);
const validation_result = validatorCtx.errors;
if (validation_result == null) {
return { success: true, data: new_value };
const disallowExtraProperties = options?.disallowExtraProperties ?? false;
const ok = validate(input, options);






if (ok) {

let p = buildParsersInput[k];
let ctx = { disallowExtraProperties };
const parsed = p(ctx, input);
return { success: true, data: parsed };
}
const errorsSlice = validation_result.slice(0, 10);
return { success: false, errors: errorsSlice };

let e = buildReportersInput[k];
let ctx = { path: [], disallowExtraProperties };
return {
success: false,
errors: e(ctx, input).slice(0, 10),
};
};
const parse = (input, options) => {
const safe = safeParse(input, options);
if (safe.success) {
return safe.data;
}
const error = new Error(`Failed to parse ${k}`);

error.errors = safe.errors;
throw error;
const explained = printErrors(safe.errors, []);
throw new Error(`Failed to parse ${k} - ${explained}`);
};
const zod = () => {

Expand All @@ -71,6 +97,7 @@ function buildParsers(args) {
safeParse,
zod,
name: k,
validate,
};
});
return decoders;
Expand Down
Loading

0 comments on commit 31241f3

Please sign in to comment.