itsyoboieltr
itsyoboieltr
Explore posts from servers
Aarktype
Created by itsyoboieltr on 9/8/2024 in #questions
Creating default object from type definition
thanks for taking your time to answer and provide detailed answers!
23 replies
Aarktype
Created by itsyoboieltr on 9/8/2024 in #questions
Creating default object from type definition
and we'll see
23 replies
Aarktype
Created by itsyoboieltr on 9/8/2024 in #questions
Creating default object from type definition
but I guess this would also be useful for others, so I'll make an issue about it
23 replies
Aarktype
Created by itsyoboieltr on 9/8/2024 in #questions
Creating default object from type definition
true
23 replies
Aarktype
Created by itsyoboieltr on 9/8/2024 in #questions
Creating default object from type definition
Maybe a better description instead of default would be something like: "simplest representation"
23 replies
Aarktype
Created by itsyoboieltr on 9/8/2024 in #questions
Creating default object from type definition
yeah, exactly. If something is optional, it shouldn't exist, if something is nullable, it should be null, etc.
23 replies
Aarktype
Created by itsyoboieltr on 9/8/2024 in #questions
Creating default object from type definition
I would expect arrays to be empty yeah. About unions I am unsure though...
23 replies
Aarktype
Created by itsyoboieltr on 9/8/2024 in #questions
Creating default object from type definition
so a good rule of thumb would be to respect the primitives, but ignore the spicy constraints (in my opinion) for default generation
23 replies
Aarktype
Created by itsyoboieltr on 9/8/2024 in #questions
Creating default object from type definition
I think it would be a great first step to support the primitive, simple use-cases, which is what most people would need this feature for. I think for the use-case I am talking about, it would be relatively simple to make something work. I would propose that we ignore regexes, length, and all the other stuff, as the generated default value is not supposed to be correct (does not need to pass type validation), as I expect the user in the frontend to update this value to make it correct at the time of submission. This is the original code, basically the reason why this feature should be implemented:
import { type } from 'arktype';

export const user = type({ name: 'string' });

export const getDefaultUser = () => ({ name: '' });
import { type } from 'arktype';

export const user = type({ name: 'string' });

export const getDefaultUser = () => ({ name: '' });
Let's say I update the user type's name property to match a regex pattern. I would still want the default user's name to have a value of an empty string, as the user will need to enter his name. The generated default name does not need to match the regex pattern.
import { type } from 'arktype';

export const user = type({ name: /^([A-Za-z]+)\s([A-Za-z]+)$/ });

export const getDefaultUser = () => ({ name: '' });
import { type } from 'arktype';

export const user = type({ name: /^([A-Za-z]+)\s([A-Za-z]+)$/ });

export const getDefaultUser = () => ({ name: '' });
Similar for length, and other properties:
import { type } from 'arktype';

export const user = type({ name: 'string > 10' });

export const getDefaultUser = () => ({ name: '' });
import { type } from 'arktype';

export const user = type({ name: 'string > 10' });

export const getDefaultUser = () => ({ name: '' });
I wouldn't expect a random 10 character string to be generated here. (By the way, in TypeBox, this would generate: 'aaaaaaaaaa') So maybe a practical solution is easier than you first thought? Of course, if your goal is to create a default value that is 100% according to the type and passes validation, then it can get complex pretty quick. But most of the time, default values are not expected to conform to the type, as they are supposed to be updated later (in the use-case of generating initial state for a form).
23 replies