Stuart B
Stuart B
Aarktype
Created by Cobain on 10/9/2024 in #questions
Function which receives a arktype's 'type'
Good to know!
13 replies
Aarktype
Created by Cobain on 10/9/2024 in #questions
Function which receives a arktype's 'type'
try:
async rateLimitedCallApi<T extends Type>(url: string, schema: T)
async rateLimitedCallApi<T extends Type>(url: string, schema: T)
13 replies
Aarktype
Created by Stuart B on 9/30/2024 in #questions
Identifying a Date value in an object type
I'm feeling guilty about causing you extra work now. But it is very much appreciated.
39 replies
Aarktype
Created by Stuart B on 9/30/2024 in #questions
Identifying a Date value in an object type
Thanks,good to know. I've just about got it working with the map() api now, where it seems to work as expected.
39 replies
Aarktype
Created by Stuart B on 9/30/2024 in #questions
Identifying a Date value in an object type
Seems like the problem wasn't what I thought it was. I tried value.json and I still have an issue. I'm iterating through a type (validator) using
validator.keyof().internal.distribute(
(key) => {
//stuff
}
validator.keyof().internal.distribute(
(key) => {
//stuff
}
And using
const value: Type = validator.get(key as never);
const value: Type = validator.get(key as never);
to get the value. This works for number, boolean, string. But when I try it with a Date, value.json returns
{ proto: '$ark.fn19' }
{ proto: '$ark.fn19' }
No sign of 'Date' anywhere. Maybe I'll have to try rewriting using .map(). (I tried before, but Typescript was giving me all sorts of problems I couldn't work out, so I stuck with the old .internal.distribute)
39 replies
Aarktype
Created by Stuart B on 9/30/2024 in #questions
Identifying a Date value in an object type
Sorry, misunderstanding. I'm not trying to identify a date ibject, I'm trying to identify a type that allows a date object.
39 replies
Aarktype
Created by Stuart B on 9/25/2024 in #questions
Post-morph constraints not checked if constraints not met on another property
Chipping in as a fairly ignorant user (of ArkType) here... I'd imagine a lot of people wanting to use ArkType as part of a form validation process. With the current behaviour, you can't get full errors to your app user about what's wrong with an input form using the obvious approach. You end up with an error or two for the user to correct, they do so, and then another one pops up. It seems the only way round that is to not use ArkType to do the data transformation from FormData strings into numeric. I don't think it's the behaviour most people would expect and is bound to cause a bunch of confusion. If it stays the way it is, I think the docs should highlight this issue and maybe provide a suggested approach for form validation, as it's such a common use case.
188 replies
Aarktype
Created by PIat on 9/4/2024 in #questions
Converting Zod to Arktype
That's great David, thanks! I think it would be worth clearly mentioning in the docs that defaults refer to the input type, regardless of where you assign them. It's pretty obvious with 'string="off"', but if you add a .default() after the morph, I expect some people will assume that it applies to the morphed type. Also, something like type('string|boolean=false') doesn't work. If there is no value passed then false as a default is not applied. If that's intentional (and I can see why it's a weird thing to do), then should it give a type error in the editor, rather than just ignore it?
174 replies
Aarktype
Created by PIat on 9/4/2024 in #questions
Converting Zod to Arktype
(and thanks)
174 replies
Aarktype
Created by PIat on 9/4/2024 in #questions
Converting Zod to Arktype
It is such a relief that it wasn't me being an idiot!
174 replies
Aarktype
Created by PIat on 9/4/2024 in #questions
Converting Zod to Arktype
Hi @ArkDavid , just to close this off, is the above example the way I need to go with this, or is there an approach that doesn't require 2 preprocess steps? I'm happy with either answer. And sorry for my ignorance, if that's what it is. You're doing a very impressive job with Arktype!
174 replies
Aarktype
Created by PIat on 9/4/2024 in #questions
Converting Zod to Arktype
I'm still missing something. The below is the simplest example of what I want to do. This works, but it requires me to generate 2 preprocessing types, one to make sure there is a string value present and the second to transform the string value to a Boolean. I can't get my head around why that can't be combined into one preprocessing step.
It seems like adding the 'string="off"' default to the second preprocess type should work, but it just ignores it. And I can't put the logic in the morph, as the morph doesn't run if it doesn't get a string value. Maybe I just have to accept "that's just the way it is, deal with it"!
// This is the shape of the data I need. I might use it for other things as well as form input.
const validator = type({
bool_value: 'boolean'
});

// These two preprocessors are just turning what I get from an HTML form checkbox into boolean
const pre_process_1 = type({
bool_value: type('string="off"')
});
const pre_process_2 = type({
bool_value: type('string').pipe((v) => (v === 'on' ? true : false))
});

const data = {
// bool_value does not exist, so I want it to be false
// if bool_value='on' then I need true. Otherwise false.
};

const preprocess_1_result = pre_process_1(data);
if (preprocess_1_result instanceof type.errors) console.log(preprocess_1_result.summary);
else console.log('preprocess 1:', preprocess_1_result);

const preprocess_2_result = pre_process_2(preprocess_1_result);
if (preprocess_2_result instanceof type.errors) console.log(preprocess_2_result.summary);
else console.log('preprocess 2:', preprocess_2_result);

const result = validator(preprocess_2_result);
if (result instanceof type.errors) console.log(result.summary);
else console.log('final:', result);
// This is the shape of the data I need. I might use it for other things as well as form input.
const validator = type({
bool_value: 'boolean'
});

// These two preprocessors are just turning what I get from an HTML form checkbox into boolean
const pre_process_1 = type({
bool_value: type('string="off"')
});
const pre_process_2 = type({
bool_value: type('string').pipe((v) => (v === 'on' ? true : false))
});

const data = {
// bool_value does not exist, so I want it to be false
// if bool_value='on' then I need true. Otherwise false.
};

const preprocess_1_result = pre_process_1(data);
if (preprocess_1_result instanceof type.errors) console.log(preprocess_1_result.summary);
else console.log('preprocess 1:', preprocess_1_result);

const preprocess_2_result = pre_process_2(preprocess_1_result);
if (preprocess_2_result instanceof type.errors) console.log(preprocess_2_result.summary);
else console.log('preprocess 2:', preprocess_2_result);

const result = validator(preprocess_2_result);
if (result instanceof type.errors) console.log(result.summary);
else console.log('final:', result);
174 replies
Aarktype
Created by PIat on 9/4/2024 in #questions
Converting Zod to Arktype
It's sort of the opposite of 1089. I can apply the morph ok but the defaults on the initial type get ignored. Is that the same cause?
174 replies
Aarktype
Created by PIat on 9/4/2024 in #questions
Converting Zod to Arktype
I know it's controversial but I find snake_case easier to read. I agree camelCase somehow looks better and is the js standard, but I find it easier to follow my code using snake_case.
174 replies
Aarktype
Created by PIat on 9/4/2024 in #questions
Converting Zod to Arktype
So what's happening is that if I don't have a 'is_male' (e.g. a checkbox is not checked) then the default is not being applied to an optional field so I end up with no 'is_male' value in the output, when I need false.
174 replies
Aarktype
Created by PIat on 9/4/2024 in #questions
Converting Zod to Arktype
I then pass this type through the function to get a modified type that is used to validate form input.
const person = type({
'name?': 'string="Dave"',
'age?': 'number>18=19',
height: 'number<300',
'is_male?': 'boolean=true'
});
const person = type({
'name?': 'string="Dave"',
'age?': 'number>18=19',
height: 'number<300',
'is_male?': 'boolean=true'
});
If I then try to validate this data:
const fake_formData_1 = {
name: 'stuart',
age: '50',
height: '180'
//is_male: ''
};
const fake_formData_1 = {
name: 'stuart',
age: '50',
height: '180'
//is_male: ''
};
I run into a problem with the default. The default for 'name' works ok. 'name' is a string input in both the original and modified types. But the defaults for age and is_male get lost, even though I have pipe the string through original with .pipe(value). That .pipe(value) is retaining the >18 number constraint on age but losing the =19. I've tried adding .default() into the function but it makes no difference. What am I missing?
174 replies
Aarktype
Created by PIat on 9/4/2024 in #questions
Converting Zod to Arktype
I'm stuck again! (It's getting a bit embarrassing now, sorry David!). This is a section of my current code.
const key_name = key.unit as never;
const value: Type = validator.get(key as never);
const non_optional_value = value.exclude('undefined');
let new_value: Type;
switch (true) {
case non_optional_value.extends(type.number):
console.log('Number field');
new_value = type('string.numeric.parse').pipe(value);
break;
case non_optional_value.extends('boolean'):
console.log('Boolean field');
new_value = type('string')
.pipe((text) => (text === 'on' ? true : false))
.pipe(value);
break;
default:
console.log('any other type of field');
new_value = non_optional_value;
}
const key_name = key.unit as never;
const value: Type = validator.get(key as never);
const non_optional_value = value.exclude('undefined');
let new_value: Type;
switch (true) {
case non_optional_value.extends(type.number):
console.log('Number field');
new_value = type('string.numeric.parse').pipe(value);
break;
case non_optional_value.extends('boolean'):
console.log('Boolean field');
new_value = type('string')
.pipe((text) => (text === 'on' ? true : false))
.pipe(value);
break;
default:
console.log('any other type of field');
new_value = non_optional_value;
}
174 replies
Aarktype
Created by PIat on 9/4/2024 in #questions
Converting Zod to Arktype
Sounds good. And once again, thanks for the help!
174 replies
Aarktype
Created by PIat on 9/4/2024 in #questions
Converting Zod to Arktype
Is there an easy(ish) way that I can force any boolean to be optional in the new type? (Because for this use case, it always will be, as I'll receive 'on' or nothing at all)
174 replies
Aarktype
Created by PIat on 9/4/2024 in #questions
Converting Zod to Arktype
My points/questions: 1. In the examples above in this thread, the "detection" of a Boolean field in the type is done by checking value.extends('boolean'). For some reason I don't understand, that doesn't work here but value.overlaps('boolean') does. My assumption here is that I'm being an idiot, but I can't work out where. 2. In Plat's conform parser I see that value.or('string.numeric.parse') is used for a number field. This works but seems to mean that and numeric constraints that were on the original numeric field definition don't apply when it's parsed from a string. So I tried type('string.numeric.parse').pipe(value), which seems to keen any original numeric constraints. 3. The below works for numbers, but Boolean is causing me problems. The original type has the Boolean as an option (as an unchecked checkbox on a form won't provide a value in formData). But the function is converting the optional key back into required. So it works if a checkbox is checked and passes 'on' but doesn't work if it is unchecked.
174 replies