@fgv/ts-utils 2.0.0 → 2.0.2-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"field.js","sourceRoot":"","sources":["../../../src/packlets/validation/field.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,kCAAiD;AAEjD,mDAAgD;AAWhD;;;;GAIG;AACH,MAAa,cAAkC,SAAQ,6BAAoB;IAczE;;;;;;;OAOG;IACH,YACE,SAAiB,EACjB,cAAgC,EAChC,OAAmC;QAEnC,KAAK,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC;QACnB,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,CAAC,cAAc,GAAG,cAAc,CAAC;QACrC,uBAAuB;QACvB,IAAI,CAAC,aAAa,GAAG,OAAO,aAAP,OAAO,cAAP,OAAO,GAAI,EAAE,CAAC;IACrC,CAAC;IAED;;OAEG;IACO,SAAS,CAAC,IAAa,EAAE,OAAY;QAC7C,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,IAAI,KAAK,IAAI,EAAE;YACrE,MAAM,QAAQ,GAAG,IAAI,CAAC,aAAa,CAAC,QAAQ,KAAK,IAAI,CAAC;YAEtD,IAAI,IAAA,cAAO,EAAC,IAAI,CAAC,SAAS,EAAE,IAAI,CAAC,EAAE;gBACjC,IAAI,CAAC,QAAQ,IAAI,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,SAAS,EAAE;oBACnD,MAAM,MAAM,GAAG,IAAI,CAAC,cAAc;yBAC/B,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,OAAO,CAAC;yBACvC,SAAS,CAAC,CAAC,OAAe,EAAE,EAAE;wBAC7B,OAAO,IAAA,WAAI,EAAC,GAAG,IAAI,CAAC,SAAS,KAAK,OAAO,EAAE,CAAC,CAAC;oBAC/C,CAAC,CAAC,CAAC;oBAEL,OAAO,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC;iBACvC;aACF;YAED,OAAO,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAA,WAAI,EAAC,IAAI,IAAI,CAAC,SAAS,0BAA0B,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;SACnG;QACD,2CAA2C;QAC3C,OAAO,IAAA,WAAI,EAAC,0BAA0B,IAAI,CAAC,SAAS,sBAAsB,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IACrG,CAAC;CACF;AA1DD,wCA0DC","sourcesContent":["/*\n * Copyright (c) 2020 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { Failure, fail, isKeyOf } from '../base';\nimport { Validator, ValidatorOptions } from './validator';\nimport { ValidatorBase } from './validatorBase';\n\n/**\n * Parameters used to construct a {@link Validation.FieldValidator | FieldValidator}.\n * @public\n */\n// eslint-disable-next-line @typescript-eslint/naming-convention\nexport interface FieldValidatorOptions<TC> extends ValidatorOptions<TC> {\n optional?: boolean;\n}\n\n/**\n * An in-place {@link Validation.Validator | Validator} for properties\n * an an object.\n * @public\n */\nexport class FieldValidator<T, TC = undefined> extends ValidatorBase<T, TC> {\n /**\n * The name of the property that this validator should validate.\n */\n public readonly fieldName: string;\n /**\n * The {@link Validation.Validator | Validator} to be applied against the named property.\n */\n public readonly fieldValidator: Validator<T, TC>;\n /**\n * @internal\n */\n protected readonly _fieldOptions: FieldValidatorOptions<TC>;\n\n /**\n * Constructs a new {@link Validation.FieldValidator | FieldValidator.}.\n * @param fieldName - The name of the property that this validator should validate.\n * @param fieldValidator - The {@link Validation.Validator | Validator} to be applied\n * against the named property.\n * @param options - Additional {@link Validation.FieldValidatorOptions | options} to be\n * applied to this validation.\n */\n public constructor(\n fieldName: string,\n fieldValidator: Validator<T, TC>,\n options?: FieldValidatorOptions<TC>\n ) {\n super({ options });\n this.fieldName = fieldName;\n this.fieldValidator = fieldValidator;\n // istanbul ignore next\n this._fieldOptions = options ?? {};\n }\n\n /**\n * {@inheritdoc Validation.ValidatorBase._validate}\n */\n protected _validate(from: unknown, context?: TC): boolean | Failure<T> {\n if (typeof from === 'object' && !Array.isArray(from) && from !== null) {\n const optional = this._fieldOptions.optional === true;\n\n if (isKeyOf(this.fieldName, from)) {\n if (!optional || from[this.fieldName] !== undefined) {\n const result = this.fieldValidator\n .validate(from[this.fieldName], context)\n .onFailure((message: string) => {\n return fail(`${this.fieldName}: ${message}`);\n });\n\n return result.success ? true : result;\n }\n }\n\n return optional ? true : fail(`\"${this.fieldName}\": Field not found in \"${JSON.stringify(from)}`);\n }\n // istanbul ignore next -- defense in depth\n return fail(`Cannot validate field '${this.fieldName}' from non-object \"${JSON.stringify(from)}\"`);\n }\n}\n"]}
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C,CAAC,MAAM,CAAC;iBACvC;aACF;YAED,OAAO,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAA,WAAI,EAAC,IAAI,IAAI,CAAC,SAAS,0BAA0B,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;SACnG;QACD,0CAA0C;QAC1C,OAAO,IAAA,WAAI,EAAC,0BAA0B,IAAI,CAAC,SAAS,sBAAsB,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IACrG,CAAC;CACF;AA1DD,wCA0DC","sourcesContent":["/*\n * Copyright (c) 2020 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { Failure, fail, isKeyOf } from '../base';\nimport { Validator, ValidatorOptions } from './validator';\nimport { ValidatorBase } from './validatorBase';\n\n/**\n * Parameters used to construct a {@link Validation.FieldValidator | FieldValidator}.\n * @public\n */\n// eslint-disable-next-line @typescript-eslint/naming-convention\nexport interface FieldValidatorOptions<TC> extends ValidatorOptions<TC> {\n optional?: boolean;\n}\n\n/**\n * An in-place {@link Validation.Validator | Validator} for properties\n * an an object.\n * @public\n */\nexport class FieldValidator<T, TC = undefined> extends ValidatorBase<T, TC> {\n /**\n * The name of the property that this validator should validate.\n */\n public readonly fieldName: string;\n /**\n * The {@link Validation.Validator | Validator} to be applied against the named property.\n */\n public readonly fieldValidator: Validator<T, TC>;\n /**\n * @internal\n */\n protected readonly _fieldOptions: FieldValidatorOptions<TC>;\n\n /**\n * Constructs a new {@link Validation.FieldValidator | FieldValidator.}.\n * @param fieldName - The name of the property that this validator should validate.\n * @param fieldValidator - The {@link Validation.Validator | Validator} to be applied\n * against the named property.\n * @param options - Additional {@link Validation.FieldValidatorOptions | options} to be\n * applied to this validation.\n */\n public constructor(\n fieldName: string,\n fieldValidator: Validator<T, TC>,\n options?: FieldValidatorOptions<TC>\n ) {\n super({ options });\n this.fieldName = fieldName;\n this.fieldValidator = fieldValidator;\n /* c8 ignore next */\n this._fieldOptions = options ?? {};\n }\n\n /**\n * {@inheritdoc Validation.ValidatorBase._validate}\n */\n protected _validate(from: unknown, context?: TC): boolean | Failure<T> {\n if (typeof from === 'object' && !Array.isArray(from) && from !== null) {\n const optional = this._fieldOptions.optional === true;\n\n if (isKeyOf(this.fieldName, from)) {\n if (!optional || from[this.fieldName] !== undefined) {\n const result = this.fieldValidator\n .validate(from[this.fieldName], context)\n .onFailure((message: string) => {\n return fail(`${this.fieldName}: ${message}`);\n });\n\n return result.success ? true : result;\n }\n }\n\n return optional ? true : fail(`\"${this.fieldName}\": Field not found in \"${JSON.stringify(from)}`);\n }\n /* c8 ignore next 2 -- defense in depth */\n return fail(`Cannot validate field '${this.fieldName}' from non-object \"${JSON.stringify(from)}\"`);\n }\n}\n"]}
@@ -67,7 +67,7 @@ class ObjectValidator extends validatorBase_1.ValidatorBase {
67
67
  const resolved = {};
68
68
  for (const key in fields) {
69
69
  if (fields[key]) {
70
- // istanbul ignore next
70
+ /* c8 ignore next */
71
71
  const optional = fields[key].isOptional || ((_a = options === null || options === void 0 ? void 0 : options.optionalFields) === null || _a === void 0 ? void 0 : _a.includes(key));
72
72
  resolved[key] = new field_1.FieldValidator(key, fields[key], { optional });
73
73
  }
@@ -84,7 +84,7 @@ class ObjectValidator extends validatorBase_1.ValidatorBase {
84
84
  * source properties.
85
85
  */
86
86
  partial(options) {
87
- // istanbul ignore next
87
+ /* c8 ignore next */
88
88
  options = options !== null && options !== void 0 ? options : {};
89
89
  return new ObjectValidator({
90
90
  fields: this.fields,
@@ -1 +1 @@
1
- {"version":3,"file":"object.js","sourceRoot":"","sources":["../../../src/packlets/validation/object.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,kCAAwC;AAExC,mDAAgF;AAEhF,mCAAyC;AA4CzC;;;;;;;GAOG;AACH,MAAa,eAAiC,SAAQ,6BAAoB;IAoBxE;;;;;;OAMG;IACH,YAAmB,MAA+C;;QAChE,KAAK,CAAC,MAAM,CAAC,CAAC;QAEd,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;QAC5B,IAAI,CAAC,OAAO,GAAG,MAAA,MAAM,CAAC,OAAO,mCAAI,EAAE,CAAC;QACpC,IAAI,CAAC,gBAAgB,GAAG,eAAe,CAAC,kBAAkB,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;QACtF,IAAI,CAAC,cAAc;YACjB,IAAI,CAAC,OAAO,CAAC,MAAM,KAAK,IAAI,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAgB,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IAChG,CAAC;IAED;;;;;;;;;;;OAWG;IACO,MAAM,CAAC,kBAAkB,CACjC,MAA8B,EAC9B,OAAuC;;QAEvC,MAAM,QAAQ,GAAoC,EAAE,CAAC;QACrD,KAAK,MAAM,GAAG,IA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;QACrD,MAAM,MAAM,GAAa,EAAE,CAAC;QAC5B,KAAK,MAAM,GAAG,IAAI,IAAI,CAAC,gBAAgB,EAAE;YACvC,IAAI,IAAI,CAAC,gBAAgB,CAAC,GAAG,CAAC,EAAE;gBAC9B,MAAM,MAAM,GAAG,IAAI,CAAC,gBAAgB,CAAC,GAAG,CAAC,CAAC,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;gBAClE,IAAI,MAAM,CAAC,OAAO,IAAI,MAAM,CAAC,KAAK,KAAK,SAAS,EAAE;oBAChD,SAAS,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,KAAK,CAAC;iBAC/B;qBAAM,IAAI,MAAM,CAAC,SAAS,EAAE,EAAE;oBAC7B,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;iBAC7B;aACF;SACF;QAED,IAAI,IAAI,CAAC,cAAc,EAAE;YACvB,MAAM,OAAO,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,cAAe,CAAC,GAAG,CAAC,CAAY,CAAC,CAAC,CAAC;YACzF,OAAO,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,GAAG,sCAAsC,CAAC,CAAC,CAAC;SACrF;QACD,OAAO,MAAM,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAA,WAAI,EAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;IAC9D,CAAC;CACF;AAjID,0CAiIC","sourcesContent":["/*\n * Copyright (c) 2021 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { Failure, fail } from '../base';\nimport { Validator, ValidatorOptions } from './validator';\nimport { ValidatorBase, ValidatorBaseConstructorParams } from './validatorBase';\n\nimport { FieldValidator } from './field';\n\n/**\n * Per-property {@link Validation.Validator | validators} for each of the properties in `<T>`.\n * @public\n */\nexport type FieldValidators<T, TC = unknown> = { [key in keyof T]: Validator<T[key], TC> };\n\n/**\n * Options for an {@link Validation.Classes.ObjectValidator | ObjectValidator}.\n * @public\n */\n// eslint-disable-next-line @typescript-eslint/naming-convention\nexport interface ObjectValidatorOptions<T, TC> extends ValidatorOptions<TC> {\n /**\n * If present, lists optional fields. Missing non-optional fields cause an error.\n */\n optionalFields?: (keyof T)[];\n /**\n * If true, unrecognized fields yield an error. If false or undefined (default),\n * unrecognized fields are ignored.\n */\n strict?: boolean;\n}\n\n/**\n * Options for the {@link Validation.Classes.ObjectValidator | ObjectValidator} constructor.\n * @public\n */\n// eslint-disable-next-line @typescript-eslint/naming-convention\nexport interface ObjectValidatorConstructorParams<T, TC> extends ValidatorBaseConstructorParams<T, TC> {\n /**\n * A {@link Validation.Classes.FieldValidators | FieldValidators} object specifying a\n * {@link Validation.Validator | Validator} for each of the expected properties\n * of a result object.\n */\n fields: FieldValidators<T>;\n /**\n * Optional additional {@link Validation.Classes.ObjectValidatorOptions | ValidatorOptions} to\n * configure validation.\n */\n options?: ObjectValidatorOptions<T, TC>;\n}\n\n/**\n * In-place {@link Validation.Validator | Validator} for an object of type `<T>`.\n * @remarks\n * By default, succeeds if all of the required fields exist and are validate, and fails if\n * any required fields do not exist or are invalid. See {@link Validation.Classes.ObjectValidatorOptions}\n * for other validation options.\n * @public\n */\nexport class ObjectValidator<T, TC = unknown> extends ValidatorBase<T, TC> {\n /**\n * A {@link Validation.Classes.FieldValidators | FieldValidators} object specifying a\n * {@link Validation.Validator | Validator} for each of the expected properties\n */\n public readonly fields: FieldValidators<T>;\n /**\n * {@link Validation.Classes.ObjectValidatorOptions | Options} which apply to this\n * validator.\n */\n public readonly options: ObjectValidatorOptions<T, TC>;\n /**\n * @internal\n */\n protected readonly _innerValidators: FieldValidators<T>;\n /**\n * @internal\n */\n protected readonly _allowedFields?: Set<keyof T>;\n\n /**\n * Constructs a new {@link Validation.Classes.ObjectValidator | ObjectValidator<T>}.\n * @param fields - A {@link Validation.Classes.FieldValidators | FieldValidators<T>} containing\n * a {@link Validation.Validator | Validator} for each field.\n * @param options - An optional {@link Validation.Classes.ObjectValidatorOptions} to configure\n * validation.\n */\n public constructor(params: ObjectValidatorConstructorParams<T, TC>) {\n super(params);\n\n this.fields = params.fields;\n this.options = params.options ?? {};\n this._innerValidators = ObjectValidator._resolveValidators(this.fields, this.options);\n this._allowedFields =\n this.options.strict === true ? new Set(Object.keys(this.fields) as (keyof T)[]) : undefined;\n }\n\n /**\n * Creates the actual {@link Validation.Classes.FieldValidators | FieldValidators<T>} to be\n * used by this converter by applying any options or traits defined in the options\n * to the field converters passed to the constructor.\n * @param fields - The base {@link Validation.Classes.FieldValidators | FieldValidators<T>} passed\n * in to the constructor.\n * @param options - The {@link Validation.Classes.ObjectValidatorOptions | object validator options}\n * passed in to the constructor.\n * @returns A new {@link Validation.Classes.FieldValidators | FieldValidators} with the fully-configured\n * individual {@link Validation.Validator | field validators} to be applied.\n * @internal\n */\n protected static _resolveValidators<T, TC>(\n fields: FieldValidators<T, TC>,\n options?: ObjectValidatorOptions<T, TC>\n ): FieldValidators<T, TC> {\n const resolved: Partial<FieldValidators<T, TC>> = {};\n for (const key in fields) {\n if (fields[key]) {\n // istanbul ignore next\n const optional = fields[key].isOptional || options?.optionalFields?.includes(key);\n resolved[key] = new FieldValidator(key, fields[key], { optional });\n }\n }\n return resolved as FieldValidators<T, TC>;\n }\n\n /**\n * Creates a new {@link Validation.Classes.ObjectValidator | ObjectValidator} derived from this one but with\n * new optional properties as specified by a supplied\n * {@link Validation.Classes.ObjectValidatorOptions | ObjectValidatorOptions<T>}.\n * @param options - The {@link Validation.Classes.ObjectValidatorOptions | options} to be applied to the new\n * {@link Validation.Classes.ObjectValidator | validator}.\n * @returns A new {@link Validation.Classes.ObjectValidator | ObjectValidator} with the additional optional\n * source properties.\n */\n public partial(options?: ObjectValidatorOptions<T, TC>): ObjectValidator<Partial<T>, TC> {\n // istanbul ignore next\n options = options ?? {};\n return new ObjectValidator<Partial<T>, TC>({\n fields: this.fields as FieldValidators<Partial<T>, TC>,\n options: {\n ...this.options,\n ...options\n },\n traits: this.traits\n });\n }\n\n /**\n * Creates a new {@link Validation.Classes.ObjectValidator | ObjectValidator} derived from this one but with\n * new optional properties as specified by a supplied array of `keyof T`.\n * @param addOptionalProperties - The keys to be made optional.\n * @returns A new {@link Validation.Classes.ObjectValidator | ObjectValidator} with the additional optional\n * source properties.\n */\n public addPartial(addOptionalFields: (keyof T)[]): ObjectValidator<Partial<T>, TC> {\n return this.partial({\n optionalFields: [...(this.options.optionalFields ?? []), ...addOptionalFields]\n });\n }\n\n /**\n * {@inheritdoc Validation.ValidatorBase._validate}\n * @internal\n */\n protected _validate(from: unknown, context?: TC): boolean | Failure<T> {\n if (typeof from !== 'object' || from === null || Array.isArray(from)) {\n return fail('source is not an object');\n }\n\n // eslint bug thinks key is used before defined\n // eslint-disable-next-line no-use-before-define\n const converted = {} as { [key in keyof T]: T[key] };\n const errors: string[] = [];\n for (const key in this._innerValidators) {\n if (this._innerValidators[key]) {\n const result = this._innerValidators[key].validate(from, context);\n if (result.success && result.value !== undefined) {\n converted[key] = result.value;\n } else if (result.isFailure()) {\n errors.push(result.message);\n }\n }\n }\n\n if (this._allowedFields) {\n const invalid = Object.keys(from).filter((k) => !this._allowedFields!.has(k as keyof T));\n invalid.forEach((key) => errors.push(`${key}: unexpected field in source object.`));\n }\n return errors.length === 0 ? true : fail(errors.join('\\n'));\n }\n}\n"]}
1
+ {"version":3,"file":"object.js","sourceRoot":"","sources":["../../../src/packlets/validation/object.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;AAEH,kCAAwC;AAExC,mDAAgF;AAEhF,mCAAyC;AA4CzC;;;;;;;GAOG;AACH,MAAa,eAAiC,SAAQ,6BAAoB;IAoBxE;;;;;;OAMG;IACH,YAAmB,MAA+C;;QAChE,KAAK,CAAC,MAAM,CAAC,CAAC;QAEd,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;QAC5B,IAAI,CAAC,OAAO,GAAG,MAAA,MAAM,CAAC,OAAO,mCAAI,EAAE,CAAC;QACpC,IAAI,CAAC,gBAAgB,GAAG,eAAe,CAAC,kBAAkB,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;QACtF,IAAI,CAAC,cAAc;YACjB,IAAI,CAAC,OAAO,CAAC,MAAM,KAAK,IAAI,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAgB,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IAChG,CAAC;IAED;;;;;;;;;;;OAWG;IACO,MAAM,CAAC,kBAAkB,CACjC,MAA8B,EAC9B,OAAuC;;QAEvC,MAAM,QAAQ,GAAoC,EAAE,CAAC;QACrD,KAAK,MAAM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AkC,CAAC;QACrD,MAAM,MAAM,GAAa,EAAE,CAAC;QAC5B,KAAK,MAAM,GAAG,IAAI,IAAI,CAAC,gBAAgB,EAAE;YACvC,IAAI,IAAI,CAAC,gBAAgB,CAAC,GAAG,CAAC,EAAE;gBAC9B,MAAM,MAAM,GAAG,IAAI,CAAC,gBAAgB,CAAC,GAAG,CAAC,CAAC,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;gBAClE,IAAI,MAAM,CAAC,OAAO,IAAI,MAAM,CAAC,KAAK,KAAK,SAAS,EAAE;oBAChD,SAAS,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,KAAK,CAAC;iBAC/B;qBAAM,IAAI,MAAM,CAAC,SAAS,EAAE,EAAE;oBAC7B,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;iBAC7B;aACF;SACF;QAED,IAAI,IAAI,CAAC,cAAc,EAAE;YACvB,MAAM,OAAO,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,cAAe,CAAC,GAAG,CAAC,CAAY,CAAC,CAAC,CAAC;YACzF,OAAO,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,GAAG,sCAAsC,CAAC,CAAC,CAAC;SACrF;QACD,OAAO,MAAM,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAA,WAAI,EAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;IAC9D,CAAC;CACF;AAjID,0CAiIC","sourcesContent":["/*\n * Copyright (c) 2021 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { Failure, fail } from '../base';\nimport { Validator, ValidatorOptions } from './validator';\nimport { ValidatorBase, ValidatorBaseConstructorParams } from './validatorBase';\n\nimport { FieldValidator } from './field';\n\n/**\n * Per-property {@link Validation.Validator | validators} for each of the properties in `<T>`.\n * @public\n */\nexport type FieldValidators<T, TC = unknown> = { [key in keyof T]: Validator<T[key], TC> };\n\n/**\n * Options for an {@link Validation.Classes.ObjectValidator | ObjectValidator}.\n * @public\n */\n// eslint-disable-next-line @typescript-eslint/naming-convention\nexport interface ObjectValidatorOptions<T, TC> extends ValidatorOptions<TC> {\n /**\n * If present, lists optional fields. Missing non-optional fields cause an error.\n */\n optionalFields?: (keyof T)[];\n /**\n * If true, unrecognized fields yield an error. If false or undefined (default),\n * unrecognized fields are ignored.\n */\n strict?: boolean;\n}\n\n/**\n * Options for the {@link Validation.Classes.ObjectValidator | ObjectValidator} constructor.\n * @public\n */\n// eslint-disable-next-line @typescript-eslint/naming-convention\nexport interface ObjectValidatorConstructorParams<T, TC> extends ValidatorBaseConstructorParams<T, TC> {\n /**\n * A {@link Validation.Classes.FieldValidators | FieldValidators} object specifying a\n * {@link Validation.Validator | Validator} for each of the expected properties\n * of a result object.\n */\n fields: FieldValidators<T>;\n /**\n * Optional additional {@link Validation.Classes.ObjectValidatorOptions | ValidatorOptions} to\n * configure validation.\n */\n options?: ObjectValidatorOptions<T, TC>;\n}\n\n/**\n * In-place {@link Validation.Validator | Validator} for an object of type `<T>`.\n * @remarks\n * By default, succeeds if all of the required fields exist and are validate, and fails if\n * any required fields do not exist or are invalid. See {@link Validation.Classes.ObjectValidatorOptions}\n * for other validation options.\n * @public\n */\nexport class ObjectValidator<T, TC = unknown> extends ValidatorBase<T, TC> {\n /**\n * A {@link Validation.Classes.FieldValidators | FieldValidators} object specifying a\n * {@link Validation.Validator | Validator} for each of the expected properties\n */\n public readonly fields: FieldValidators<T>;\n /**\n * {@link Validation.Classes.ObjectValidatorOptions | Options} which apply to this\n * validator.\n */\n public readonly options: ObjectValidatorOptions<T, TC>;\n /**\n * @internal\n */\n protected readonly _innerValidators: FieldValidators<T>;\n /**\n * @internal\n */\n protected readonly _allowedFields?: Set<keyof T>;\n\n /**\n * Constructs a new {@link Validation.Classes.ObjectValidator | ObjectValidator<T>}.\n * @param fields - A {@link Validation.Classes.FieldValidators | FieldValidators<T>} containing\n * a {@link Validation.Validator | Validator} for each field.\n * @param options - An optional {@link Validation.Classes.ObjectValidatorOptions} to configure\n * validation.\n */\n public constructor(params: ObjectValidatorConstructorParams<T, TC>) {\n super(params);\n\n this.fields = params.fields;\n this.options = params.options ?? {};\n this._innerValidators = ObjectValidator._resolveValidators(this.fields, this.options);\n this._allowedFields =\n this.options.strict === true ? new Set(Object.keys(this.fields) as (keyof T)[]) : undefined;\n }\n\n /**\n * Creates the actual {@link Validation.Classes.FieldValidators | FieldValidators<T>} to be\n * used by this converter by applying any options or traits defined in the options\n * to the field converters passed to the constructor.\n * @param fields - The base {@link Validation.Classes.FieldValidators | FieldValidators<T>} passed\n * in to the constructor.\n * @param options - The {@link Validation.Classes.ObjectValidatorOptions | object validator options}\n * passed in to the constructor.\n * @returns A new {@link Validation.Classes.FieldValidators | FieldValidators} with the fully-configured\n * individual {@link Validation.Validator | field validators} to be applied.\n * @internal\n */\n protected static _resolveValidators<T, TC>(\n fields: FieldValidators<T, TC>,\n options?: ObjectValidatorOptions<T, TC>\n ): FieldValidators<T, TC> {\n const resolved: Partial<FieldValidators<T, TC>> = {};\n for (const key in fields) {\n if (fields[key]) {\n /* c8 ignore next */\n const optional = fields[key].isOptional || options?.optionalFields?.includes(key);\n resolved[key] = new FieldValidator(key, fields[key], { optional });\n }\n }\n return resolved as FieldValidators<T, TC>;\n }\n\n /**\n * Creates a new {@link Validation.Classes.ObjectValidator | ObjectValidator} derived from this one but with\n * new optional properties as specified by a supplied\n * {@link Validation.Classes.ObjectValidatorOptions | ObjectValidatorOptions<T>}.\n * @param options - The {@link Validation.Classes.ObjectValidatorOptions | options} to be applied to the new\n * {@link Validation.Classes.ObjectValidator | validator}.\n * @returns A new {@link Validation.Classes.ObjectValidator | ObjectValidator} with the additional optional\n * source properties.\n */\n public partial(options?: ObjectValidatorOptions<T, TC>): ObjectValidator<Partial<T>, TC> {\n /* c8 ignore next */\n options = options ?? {};\n return new ObjectValidator<Partial<T>, TC>({\n fields: this.fields as FieldValidators<Partial<T>, TC>,\n options: {\n ...this.options,\n ...options\n },\n traits: this.traits\n });\n }\n\n /**\n * Creates a new {@link Validation.Classes.ObjectValidator | ObjectValidator} derived from this one but with\n * new optional properties as specified by a supplied array of `keyof T`.\n * @param addOptionalProperties - The keys to be made optional.\n * @returns A new {@link Validation.Classes.ObjectValidator | ObjectValidator} with the additional optional\n * source properties.\n */\n public addPartial(addOptionalFields: (keyof T)[]): ObjectValidator<Partial<T>, TC> {\n return this.partial({\n optionalFields: [...(this.options.optionalFields ?? []), ...addOptionalFields]\n });\n }\n\n /**\n * {@inheritdoc Validation.ValidatorBase._validate}\n * @internal\n */\n protected _validate(from: unknown, context?: TC): boolean | Failure<T> {\n if (typeof from !== 'object' || from === null || Array.isArray(from)) {\n return fail('source is not an object');\n }\n\n // eslint bug thinks key is used before defined\n // eslint-disable-next-line no-use-before-define\n const converted = {} as { [key in keyof T]: T[key] };\n const errors: string[] = [];\n for (const key in this._innerValidators) {\n if (this._innerValidators[key]) {\n const result = this._innerValidators[key].validate(from, context);\n if (result.success && result.value !== undefined) {\n converted[key] = result.value;\n } else if (result.isFailure()) {\n errors.push(result.message);\n }\n }\n }\n\n if (this._allowedFields) {\n const invalid = Object.keys(from).filter((k) => !this._allowedFields!.has(k as keyof T));\n invalid.forEach((key) => errors.push(`${key}: unexpected field in source object.`));\n }\n return errors.length === 0 ? true : fail(errors.join('\\n'));\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fgv/ts-utils",
3
- "version": "2.0.0",
3
+ "version": "2.0.2-alpha.0",
4
4
  "description": "Assorted Typescript Utilities",
5
5
  "main": "lib/index.js",
6
6
  "types": "dist/ts-utils.d.ts",
@@ -28,34 +28,34 @@
28
28
  "homepage": "https://github.com/ErikFortune/fgv#readme",
29
29
  "devDependencies": {
30
30
  "@jest/expect-utils": "^29.5.0",
31
- "@microsoft/api-documenter": "^7.21.6",
32
- "@microsoft/api-extractor": "^7.34.4",
33
- "@types/jest": "^29.5.0",
34
- "@types/luxon": "^3.2.0",
31
+ "@microsoft/api-documenter": "^7.22.14",
32
+ "@microsoft/api-extractor": "^7.35.2",
33
+ "@types/jest": "^29.5.2",
34
+ "@types/luxon": "^3.3.0",
35
35
  "@types/mustache": "^4.2.2",
36
- "@types/node": "^18.15.10",
36
+ "@types/node": "^20.2.5",
37
37
  "@types/papaparse": "^5.3.7",
38
- "@typescript-eslint/eslint-plugin": "^5.56.0",
39
- "@typescript-eslint/parser": "^5.56.0",
40
- "eslint": "^8.36.0",
41
- "eslint-config-standard": "^17.0.0",
38
+ "@typescript-eslint/eslint-plugin": "^5.59.9",
39
+ "@typescript-eslint/parser": "^5.59.9",
40
+ "eslint": "^8.42.0",
41
+ "eslint-config-standard": "^17.1.0",
42
42
  "eslint-plugin-import": "^2.27.5",
43
43
  "eslint-plugin-node": "^11.1.0",
44
44
  "eslint-plugin-promise": "^6.1.1",
45
45
  "jest": "^29.5.0",
46
- "jest-extended": "^3.2.4",
46
+ "jest-extended": "^4.0.0",
47
47
  "jest-matcher-utils": "^29.5.0",
48
- "rimraf": "^4.4.1",
49
- "ts-jest": "^29.0.5",
48
+ "rimraf": "^5.0.1",
49
+ "ts-jest": "^29.1.0",
50
50
  "ts-node": "^10.9.1",
51
- "typescript": "^4.9.5",
52
- "eslint-plugin-n": "^15.0.0",
51
+ "typescript": "^5.1.3",
52
+ "eslint-plugin-n": "^16.0.0",
53
53
  "jest-snapshot": "~29.5.0",
54
- "@rushstack/heft": "~0.50.0",
55
- "@rushstack/heft-node-rig": "~1.12.5",
56
- "@rushstack/eslint-config": "~3.2.0",
54
+ "@rushstack/heft": "~0.53.0",
55
+ "@rushstack/heft-node-rig": "~2.2.3",
56
+ "@rushstack/eslint-config": "~3.3.1",
57
57
  "@types/heft-jest": "1.0.3",
58
- "@rushstack/heft-jest-plugin": "~0.5.5",
58
+ "@rushstack/heft-jest-plugin": "~0.7.4",
59
59
  "eslint-plugin-tsdoc": "~0.2.17"
60
60
  },
61
61
  "dependencies": {