UNPKG

bguard

Version:

**bguard** is a powerful, flexible, and type-safe validation library for TypeScript. It allows developers to define validation schemas for their data structures and ensures that data conforms to the expected types and constraints.

1 lines 2.02 kB
{"version":3,"sources":["../src/asserts/array/index.ts"],"sourcesContent":["import { type WithArray } from '../../commonTypes';\nimport { BuildSchemaError } from '../../exceptions';\nimport { ctxSymbol } from '../../helpers/constants';\nimport { CommonSchema, type ValidatorContext } from '../../core';\n\n/**\n * @description Creates a new schema for validating arrays where each element must match the specified schema.\n * @template T\n * @param {T} arraySchema - The schema that each element of the array must match.\n * @returns {WithArray<ArraySchema, T>} A new instance of `ArraySchema` for validating arrays of elements that match the specified schema.\n * @example\n * const schema = array(string());\n * parseOrFail(schema, ['hello', 'world']); // Validates successfully\n * parseOrFail(schema, ['hello', 123]); // Throws a validation error\n *\n * @instance Of ArraySchema\n */\nexport function array<T extends CommonSchema>(arraySchema: T): WithArray<ArraySchema, T> {\n return new ArraySchema({ type: [], requiredValidations: [] }, arraySchema) as WithArray<ArraySchema, T>;\n}\n\nclass ArraySchema extends CommonSchema {\n protected _array = 1;\n constructor(ctx: ValidatorContext, arraySchema: CommonSchema) {\n super(ctx);\n this.validateArrayEntry(arraySchema);\n this[ctxSymbol].array = arraySchema;\n }\n\n private validateArrayEntry(arraySchema: CommonSchema) {\n if (!arraySchema) throw new BuildSchemaError('Missing schema in array method');\n if (!(arraySchema instanceof CommonSchema)) throw new BuildSchemaError('Invalid schema in array method');\n }\n}\n"],"mappings":";;;;;;;;;;;AAiBO,SAAS,MAA8B,aAA2C;AACvF,SAAO,IAAI,YAAY,EAAE,MAAM,CAAC,GAAG,qBAAqB,CAAC,EAAE,GAAG,WAAW;AAC3E;AAEA,IAAM,cAAN,cAA0B,aAAa;AAAA,EAC3B,SAAS;AAAA,EACnB,YAAY,KAAuB,aAA2B;AAC5D,UAAM,GAAG;AACT,SAAK,mBAAmB,WAAW;AACnC,SAAK,SAAS,EAAE,QAAQ;AAAA,EAC1B;AAAA,EAEQ,mBAAmB,aAA2B;AACpD,QAAI,CAAC,YAAa,OAAM,IAAI,iBAAiB,gCAAgC;AAC7E,QAAI,EAAE,uBAAuB,cAAe,OAAM,IAAI,iBAAiB,gCAAgC;AAAA,EACzG;AACF;","names":[]}