Skip to content

Commit

Permalink
Agrego recordString
Browse files Browse the repository at this point in the history
  • Loading branch information
emilioplatzer committed May 2, 2024
1 parent bf10995 commit 192104c
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 18 deletions.
26 changes: 13 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "guarantee-type",
"description": "guarantee the type of a plain object",
"version": "0.1.2",
"version": "0.1.3",
"repository": "emilioplatzer/guarantee-type",
"files": [
"lib",
Expand Down Expand Up @@ -33,10 +33,10 @@
},
"devDependencies": {
"@types/mocha": "^10.0.6",
"@types/node": "^20.11.17",
"@types/node": "^20.12.7",
"expect.js": "^0.3.1",
"mocha": "^10.3.0",
"mocha": "^10.4.0",
"nyc": "^15.1.0",
"typescript": "^5.3.3"
"typescript": "^5.4.5"
}
}
19 changes: 18 additions & 1 deletion src/lib/guarantee-type.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export type Literal = number | string | boolean | null
export type Values = 'string'|'number'|'boolean'|'bigint'|'symbol'
export type Keys = Values | 'nullable' | 'optional' | 'object' | 'array' | 'class' | 'union' | 'literal'
export type Keys = Values | 'recordString' | 'nullable' | 'optional' | 'object' | 'array' | 'class' | 'union' | 'literal'

export type Opts = {};

Expand All @@ -10,6 +10,7 @@ export type Description =
{ boolean: Opts } |
{ bigint : Opts } |
{ symbol : Opts } |
{ recordString : Description } |
{ nullable: Description } |
{ optional: Description } |
{ object: {[K in keyof any]: Description} } |
Expand All @@ -30,6 +31,7 @@ export type DefinedType<Description> =
Description extends { optional: infer T } ? DefinedType<T>|null|undefined :
Description extends { object: infer T} ? {[K in keyof T] : DefinedType<T[K]>} :
Description extends { array: infer T} ? DefinedType<T>[] :
Description extends { recordString : infer T } ? Record<string, DefinedType<T>> :
Description extends { union: (infer T1) [] } ? DefinedType<T1> :
Description extends { class: infer T } ? ( T extends Constructor<any> ? InstanceType<T> : unknown ) :
Description extends { literal: (infer T1 extends string | number | boolean | null) } ? T1 :
Expand All @@ -49,6 +51,13 @@ export var errorTypeFinder = {
boolean:valueGuarantor('boolean'),
bigint :valueGuarantor('bigint'),
symbol :valueGuarantor('symbol'),
recordString: function(innerDescription:Description, value:any, path:string, errors:string[]){
if(!(value instanceof Array) && (value instanceof Object) && value){
for (var a in value) {
findErrorsInTypes(innerDescription, value[a], path+`[${a}]`, errors);
}
}else errors.push(`${path} is not a Record<string,T> and must be`);
},
object: function(innerDescription:{[K:string] : Description}, value:any, path:string, errors:string[]){
for ( var a in innerDescription ){
findErrorsInTypes(innerDescription[a], value[a], path+'.'+a, errors);
Expand Down Expand Up @@ -205,6 +214,12 @@ type IS2 = IS1 & {
}

type IS = IS2 & {
recordString: {[k in keyof IS1]: {recordString:Pick<IS1,k>}} & {
nullable : {[k in keyof IS1]: {recordString:{nullable:Pick<IS1,k>}}},
optional : {[k in keyof IS1]: {recordString:{optional:Pick<IS1,k>}}},
} & {
object:<T>(descriptions:T)=>( {recordString:{object:T}} )
},
nullable : {[k in keyof IS1]: {nullable:Pick<IS1,k>}} & {
array : {[k in keyof IS1]: {nullable:{array:Pick<IS1,k>}}},
} & {
Expand Down Expand Up @@ -248,6 +263,8 @@ export var is:IS = {
bigint : {bigint : {}},
symbol : {symbol : {}},
// @ts-ignore TODO!!!!
get recordString(){ return isModificator(['recordString'])},
// @ts-ignore TODO!!!!
get nullable(){ return isModificator(['nullable'])},
// @ts-ignore TODO!!!!
get optional(){ return isModificator(['optional'])},
Expand Down
29 changes: 29 additions & 0 deletions src/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,35 @@ describe("guarantee",function(){
assert.throws(()=>guarantee(description, {omega:false}), /guarantee excpetion. Value\.omega is not an array and must be/);
})
})
describe("recordString", function(){
it("accept recordString", function(){
var description = {recordString:{boolean:opts}};
var result:Record<string, boolean> = guarantee(description, {yes:true, no:false});
var autoResult = guarantee(is.recordString.boolean, {yes:true, no:false}); // to ensure not 'any'
// @ts-expect-error
var wrongResult:Record<string, string> = autoResult // if the previous return 'any' this don't detect the error
var autoResult2 = guarantee(is.recordString.optional.boolean, {yes:true, no:false}); // to ensure not 'any'
var rightResult2:Record<string, (boolean|undefined|null)> = autoResult2
// @ts-expect-error
var wrongResult2:Record<string, boolean> = autoResult2 // if the previous return 'any' this don't detect the error
})
it("rejects non recordString", function(){
var description = {recordString:{boolean:opts}};
assert.throws(()=>guarantee(description, true), /guarantee excpetion. Value is not a Record<string,T> and must be/);
})
it("rejects wrong element", function(){
var description = {array:{boolean:opts}};
assert.throws(()=>guarantee(description, [true,'one']), /guarantee excpetion. Value\[1\] is not "boolean"/);
})
it("rejects wrong element in an object with recordString", function(){
var description = {object:{omega:{recordString:{boolean:opts}}}};
assert.throws(()=>guarantee(description, {omega:{one:[]}}), /guarantee excpetion. Value\.omega\[one\] is not "boolean"/);
})
it("rejects non recordString in object", function(){
var description = {object:{omega:{recordString:{boolean:opts}}}};
assert.throws(()=>guarantee(description, {omega:false}), /guarantee excpetion. Value\.omega is not a Record<string,T> and must be/);
})
})
describe("union", function(){
it("accepts any type with the first value", function(){
var result: string|number;
Expand Down

0 comments on commit 192104c

Please sign in to comment.