A
arktype2mo ago
Arold

Augmenting the default type scope

I'm trying to augment the default scope with additional types and re-export it so that it maintains all the built-in aliases for the subtypes that have been augmented. The ...type.keywords.string spreading mentioned in https://discord.com/channels/957797212103016458/1414659167008063588 just ain't doing it for me and I can't explain why. Here's what I've been trying, the compile-time failure case is at the bottom of this snippet -
3 Replies
Arold
AroldOP2mo ago
import { scope, Type, type } from 'arktype';
import normalizeUrl from 'normalize-url';

const overrideScope = scope({
string: type.module({
...type.keywords.string,
url: type.module({
...type.keywords.string.url,
github: type.module({
root: /^https:\/\/github\.com\//,
profile: type.module({
root: /^https:\/\/github\.com\/[^/]+$/,
extract: ['string.url', '=>', (url: string) =>
normalizeUrl(url, {
transformPath: (pathComponents) =>
pathComponents.slice(0, 1),
})],
}),
repo: type.module({
root: /^https:\/\/github\.com\/[^/]+\/[^/]+$/,
extract: ['string.url', '=>', (url) =>
normalizeUrl(url, {
transformPath: (pathComponents) =>
pathComponents.slice(0, 2),
})],
}),
}),
}),
}),
});

console.log(
overrideScope.type('string.url.github.repo.extract').assert(
'https://github.com/account/repo/butts/butts/butts',
),
);
// works, prints https://github.com/account/repo

console.log(overrideScope.type('boolean').assert('true')); // works

const overridescopeStringStillExists = overrideScope.type(
'string.number',
// └─Argument of type '"string.number"' is not
// assignable to parameter of type '"'string.number'
// is unresolvable "'.
);
import { scope, Type, type } from 'arktype';
import normalizeUrl from 'normalize-url';

const overrideScope = scope({
string: type.module({
...type.keywords.string,
url: type.module({
...type.keywords.string.url,
github: type.module({
root: /^https:\/\/github\.com\//,
profile: type.module({
root: /^https:\/\/github\.com\/[^/]+$/,
extract: ['string.url', '=>', (url: string) =>
normalizeUrl(url, {
transformPath: (pathComponents) =>
pathComponents.slice(0, 1),
})],
}),
repo: type.module({
root: /^https:\/\/github\.com\/[^/]+\/[^/]+$/,
extract: ['string.url', '=>', (url) =>
normalizeUrl(url, {
transformPath: (pathComponents) =>
pathComponents.slice(0, 2),
})],
}),
}),
}),
}),
});

console.log(
overrideScope.type('string.url.github.repo.extract').assert(
'https://github.com/account/repo/butts/butts/butts',
),
);
// works, prints https://github.com/account/repo

console.log(overrideScope.type('boolean').assert('true')); // works

const overridescopeStringStillExists = overrideScope.type(
'string.number',
// └─Argument of type '"string.number"' is not
// assignable to parameter of type '"'string.number'
// is unresolvable "'.
);
ssalbdivad
ssalbdivad2mo ago
this looks right to me the name of the builtin keyword is string.numeric not string.number
Arold
AroldOP2mo ago
wow lol all green now, thank you

Did you find this page helpful?