MCPcopy
hub / github.com/vuejs/core / validateProp

Function validateProp

packages/runtime-core/src/componentProps.ts:677–714  ·  view source on GitHub ↗

* dev only

(
  name: string,
  value: unknown,
  prop: PropOptions,
  props: Data,
  isAbsent: boolean,
)

Source from the content-addressed store, hash-verified

675 * dev only
676 */
677function validateProp(
678 name: string,
679 value: unknown,
680 prop: PropOptions,
681 props: Data,
682 isAbsent: boolean,
683) {
684 const { type, required, validator, skipCheck } = prop
685 // required!
686 if (required && isAbsent) {
687 warn('Missing required prop: "' + name + '"')
688 return
689 }
690 // missing but optional
691 if (value == null && !required) {
692 return
693 }
694 // type check
695 if (type != null && type !== true && !skipCheck) {
696 let isValid = false
697 const types = isArray(type) ? type : [type]
698 const expectedTypes = []
699 // value is valid as long as one of the specified types match
700 for (let i = 0; i < types.length && !isValid; i++) {
701 const { valid, expectedType } = assertType(value, types[i])
702 expectedTypes.push(expectedType || '')
703 isValid = valid
704 }
705 if (!isValid) {
706 warn(getInvalidTypeMessage(name, value, expectedTypes))
707 return
708 }
709 }
710 // custom validator
711 if (validator && !validator(value, props)) {
712 warn('Invalid prop: custom validator check failed for prop "' + name + '".')
713 }
714}
715
716const isSimpleType = /*@__PURE__*/ makeMap(
717 'String,Number,Boolean,Function,Symbol,BigInt',

Callers 1

validatePropsFunction · 0.85

Calls 5

warnFunction · 0.90
assertTypeFunction · 0.85
getInvalidTypeMessageFunction · 0.85
validatorFunction · 0.85
pushMethod · 0.65

Tested by

no test coverage detected